Skip to content

Instantly share code, notes, and snippets.

@jcarbaugh
Created April 23, 2009 18:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jcarbaugh/100651 to your computer and use it in GitHub Desktop.
Save jcarbaugh/100651 to your computer and use it in GitHub Desktop.
convert a MySQL dump into SQLite
#!/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)
@thread13
Copy link

Hi, thanks a lot for your work, but obviously you did not have a big database:

===
replacement = ''
for vals in INSERTVALS_SPLIT_RE.split(values):
    replacement = '%s\n%s (%s);' % (replacement, insert, vals)
return replacement
>>>
replacement = []
for vals in INSERTVALS_SPLIT_RE.split(values):
    replacement.append( '%s (%s);' % (insert, vals) )
return '\n'.join( replacement )
===

For any data of decent size, these quadratic algorithms will be a pain.

Few more suggestions:

===
INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*\((.*)\);$', re.I | re.M | re.S)
=>
INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*\((.*?)\);$', re.M | re.S)
===

( we want more than one table, don't we ? )
-- although this will of course always be the risky part,

and may be add

===
content = content.replace( r"\'", "''" )
===

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment