Skip to content

Instantly share code, notes, and snippets.

@flashingpumpkin
Created January 24, 2011 12:25
Show Gist options
  • Save flashingpumpkin/793150 to your computer and use it in GitHub Desktop.
Save flashingpumpkin/793150 to your computer and use it in GitHub Desktop.
Move a MySQL dump to SQLite
# Modified from: http://stackoverflow.com/questions/489277/script-to-convert-mysql-dump-sql-file-into-format-that-can-be-imported-into-sqlit
# Dump: mysqldump -u root -p --compatible=ansi <DatabaseName> > db.sql
# Convert & import: mysql_to_sqlite.sh db.sql | sqlite3 db.sqlite
# This works on my setup - you might have to modified the pipes!
#!/bin/bash
if [ "$1" == "-h" ];
then
echo "Usage: $0 <dumpname>";
exit 1;
fi
cat $1 |
grep -v ' KEY "' |
grep -v ' UNIQUE KEY "' |
grep -v ' PRIMARY KEY ' |
grep -v ' AUTO_INCREMENT ' |
grep -v 'LOCK TABLES' |
grep -v 'UNLOCK TABLES' |
sed '/^SET/d' |
sed -r 's/^.*UNLOCK TABLES.*//ig' |
sed -r 's/^.*LOCK TABLES.*//ig' |
sed -r 's/ unsigned / /g' |
sed -r 's/ auto_increment/ PRIMARY KEY /ig' |
sed -r 's/ smallint([0-9]*) / integer /ig' |
sed -r 's/ tinyint([0-9]*) / integer /ig' |
sed -r 's/ int([0-9]*) / integer /ig' |
sed -r 's/ character set [^ ]* / /ig' |
sed -r 's/ enum([^)]*) / varchar(255) /ig' |
sed -r 's/ on update [^,]*//ig' |
perl -e 'local $/;$_=<>;s/,\n\)/\n\)/gs;print "begin;\n";print;print "commit;\n"' |
perl -pe '
if (/^(INSERT.+?)\(/) {
$a=$1;
s/\\'\''/'\'\''/g;
s/\\n/\n/g;
s/\),\(/\);\n$a\(/g;
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment