sqlite3 databse.db .dump > sqlite3_database_dump.sql
python sqlite3_to_mysql.py database.sql > mysql_database_dump.sql
Last active
April 11, 2018 03:48
-
-
Save ekinertac/f2021b99796928f0726e825b879890b5 to your computer and use it in GitHub Desktop.
Converts sqlite3 database dumps to mysql dump format
This file contains 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
import re, fileinput | |
def main(): | |
for line in fileinput.input(): | |
process = False | |
for nope in ('BEGIN TRANSACTION','COMMIT', 'sqlite_sequence','CREATE UNIQUE INDEX'): | |
if nope in line: break | |
else: | |
process = True | |
if not process: continue | |
m = re.search('CREATE TABLE "([a-z_]*)"(.*)', line) | |
if m: | |
name, sub = m.groups() | |
line = '''DROP TABLE IF EXISTS %(name)s; | |
CREATE TABLE IF NOT EXISTS %(name)s%(sub)s | |
''' | |
line = line % dict(name=name, sub=sub) | |
else: | |
m = re.search('INSERT INTO "([a-z_]*)"(.*)', line) | |
if m: | |
line = 'INSERT INTO %s%s\n' % m.groups() | |
line = line.replace('"', r'\"') | |
line = line.replace('"', "'") | |
line = re.sub(r"([^'])'t'(.)", r"\1THIS_IS_TRUE\2", line) | |
line = line.replace('THIS_IS_TRUE', '1') | |
line = re.sub(r"([^'])'f'(.)", r"\1THIS_IS_FALSE\2", line) | |
line = line.replace('THIS_IS_FALSE', '0') | |
line = line.replace('AUTOINCREMENT', 'AUTO_INCREMENT') | |
print line, | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment