Created
April 23, 2009 18:12
-
-
Save jcarbaugh/100651 to your computer and use it in GitHub Desktop.
convert a MySQL dump into SQLite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Convert a mysql dump into a sqlite-compatible format. | |
# I wrote this for just one script... no guarantess that it will work with others... | |
# python fsql.py < mysqldump.sql > readyforsqlite.sql | |
import re | |
import sys | |
content = sys.stdin.read() | |
# unused commands | |
COMMAND_RE = re.compile(r'^(SET).*?;\n$', re.I | re.M | re.S) | |
content = COMMAND_RE.sub('', content) | |
# table constraints | |
TCONS_RE = re.compile(r'\)(\s*(CHARSET|DEFAULT|ENGINE)(=.*?)?\s*)+;', re.I | re.M | re.S) | |
content = TCONS_RE.sub(');', content) | |
# insert multiple values | |
INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*\((.*)\);$', re.I | re.M | re.S) | |
INSERTVALS_SPLIT_RE = re.compile(r'\)\s*,\s*\(', re.I | re.M | re.S) | |
def insertvals_replacer(match): | |
insert, values = match.groups() | |
replacement = '' | |
for vals in INSERTVALS_SPLIT_RE.split(values): | |
replacement = '%s\n%s (%s);' % (replacement, insert, vals) | |
return replacement | |
content = INSERTVALS_RE.sub(insertvals_replacer, content) | |
# write results to stdout | |
sys.stdout.write(content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks a lot for your work, but obviously you did not have a big database:
For any data of decent size, these quadratic algorithms will be a pain.
Few more suggestions:
( we want more than one table, don't we ? )
-- although this will of course always be the risky part,
and may be add
to change the way MySQL escapes quotes to SQLite-compatible.
( I have forked your great example to handle a few MySQL dumps of my own, but the fork is also quite specific and certainly not a general solution. )
Best,
/t13