Skip to content

Instantly share code, notes, and snippets.

@orip
Forked from orenhe/mysqldump2sqlite3.sh
Created September 23, 2012 15:23
Show Gist options
  • Save orip/3772021 to your computer and use it in GitHub Desktop.
Save orip/3772021 to your computer and use it in GitHub Desktop.
Convert a mysql database dump into something sqlite3 understands.
#!/bin/sh
# ================================================================
#
# Convert a mysql database dump into something sqlite3 understands.
#
# Adapted from
# http://stackoverflow.com/questions/489277/script-to-convert-mysql-dump-sql-file-into-format-that-can-be-imported-into-sqlit
#
# (c) 2010 Martin Czygan <martin.czygan@gmail.com>
#
# ================================================================
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <dumpname>"
exit
fi
SRC=$1
DST=$1.sqlite3.sql
DB=$1.sqlite3.db
ERR=$1.sqlite3.err
cat $SRC |
grep -v ' KEY "' |
grep -v ' UNIQUE KEY "' |
grep -v ' PRIMARY KEY ' |
perl -pe 's/--.*$//g' | # single line comments break line joining we do later
perl -pe 's/ENGINE=MyISAM/ /g' |
perl -pe 's/ENGINE=InnoDB/ /g' |
#perl -pe 's/DEFAULT/ /g' |
perl -pe 's/DEFAULT CHARSET=[a-zA-Z0-9]*/ /g' |
perl -pe 's/AUTO_INCREMENT=[0-9]*/ /g' |
perl -pe 's/\\r\\n/\\n/g' |
perl -pe 's/\\"/"/g' |
grep -v '/^SET/' |
perl -pe 's/ unsigned / /g' |
perl -pe 's/ auto_increment/ primary key autoincrement/ig' |
perl -pe 's/ smallint\([0-9]*\) / integer /g' |
perl -pe 's/ tinyint\([0-9]*\) / integer /g' |
perl -pe 's/ int\([0-9]*\) / integer /g' |
perl -pe 's/ character set [^ ]* / /g' |
perl -pe 's/ enum\([^)]*\) / varchar\(255\) /g' |
perl -pe 's/ on update [^,]*//g' |
perl -pe 's/UNLOCK TABLES;//g' |
perl -pe 's/LOCK TABLES [^;]*;//g' |
perl -e 'local $/;$_=<>;s/,\n\)/\n\)/gs;print "begin;\n";print;print "commit;\n"' |
grep -v "\<KEY\>" |
perl -pe '
if (/^(INSERT.+?)\(/) {
$a=$1;
s/\\'\''/'\'\''/g;
s/\\n/\n/g;
s/\),\(/\);\n$a\(/g;
}
' |
python -c '
import sys
sys.stdout.write(sys.stdin.read().replace("\n", " ").replace("; ", ";\n").replace(", )", ")"))
sys.stdout.flush()
' |
perl -pe 's/, \)/\)/g' > $DST
echo TODO REMOVE LINES WITH KEY/UNIQUE KEY *and previous comma*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment