Skip to content

Instantly share code, notes, and snippets.

@vilmosioo
Created April 21, 2013 02:41
Show Gist options
  • Save vilmosioo/5428248 to your computer and use it in GitHub Desktop.
Save vilmosioo/5428248 to your computer and use it in GitHub Desktop.
Recently I had to transfer an sqlite database to mysql. Unfortunately, there are a few minor differences between the dump files. This PHP script will try and transform the sqlite dump file to a mysql dump file. Feel free to contribute.
<?php
$read = @fopen("dump_sqlite", "r");
$write = @fopen("dump_mysql", "w");
if ($read && $write) {
while (($buffer = fgets($read, 4096)) !== false) {
// replace all double-quotes (") with grave accents (')
$buffer = preg_replace('/"/', "'", $buffer);
// replace "autoincrement" with "auto_increment"
$buffer = str_replace("autoincrement", "auto_increment", $buffer);
// remove BEGIN TRANSACTION;
$buffer = str_replace("BEGIN TRANSACTION;", "", $buffer);
// remove COMMIT;
$buffer = str_replace("COMMIT;", "", $buffer);
// remove quotes from create table
if(is_numeric(strpos($buffer, "CREATE TABLE"))){
$buffer = str_replace("'", "", $buffer);
}
// remove quotes from table name
if(is_numeric(strpos($buffer, "INSERT INTO"))){
$buffer = explode("(", $buffer, 2);
$buffer = str_replace("'", "", $buffer[0]) .'('. $buffer[1];
}
fwrite($write, $buffer);
}
if (!feof($read)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($read);
fclose($write);
echo 'Complete.';
} else {
echo 'Could not open file.<br>Read: '. $read . '<br>Write: ' . $write;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment