Skip to content

Instantly share code, notes, and snippets.

@kevin-brown
Last active December 7, 2017 01:50
Show Gist options
  • Save kevin-brown/d5fd25cd67e42a0d6261 to your computer and use it in GitHub Desktop.
Save kevin-brown/d5fd25cd67e42a0d6261 to your computer and use it in GitHub Desktop.
MySQL to SQLite database dump converter
import fileinput
import re
import sys
file_name = sys.argv[1]
new_file = sys.argv[2]
rh = open(file_name, "r")
lines = rh.readlines()
rh.close()
wh = open(new_file, "w")
table_name = ""
NAME_RE = re.compile("`(.*?)`")
INSERT_RE = re.compile("\),\(")
wh.write("PRAGMA synchronous = OFF;\n")
wh.write("PRAGMA journal_mode = MEMORY;\n")
wh.write("BEGIN TRANSACTION;\n")
creating_table = False
keys = []
for line in lines:
if line.startswith(")") and creating_table:
creating_table = False
line = ");"
if line.startswith("LOCK") or line.startswith("UNLOCK"):
continue
if line.startswith("CREATE TABLE"):
creating_table = True
table_name = re.search(NAME_RE, line).group(1)
if "AUTO_INCREMENT" in line and creating_table:
line = line.replace("AUTO_INCREMENT", "")
if "KEY" in line and creating_table:
if "CONSTRAINT" in line:
continue
if "PRIMARY KEY" in line:
line = line.replace("),", ")")
else:
names = re.findall(NAME_RE, line)
name, field = names
keys.append((name, field))
line = ""
if line.startswith("INSERT"):
parts = re.split(INSERT_RE, line)
line = line.replace("\\'", "''")
line = line.replace("\\'',", "\\',")
line = line.replace("\\n", "\n")
line = line.replace("\\r", "\r")
line = line.replace("\\\"", "\"")
line = line.replace("\\\\", "\\")
line = line.replace("\ ", " ")
splits = parts[1::240]
for split in splits:
fixed_split = ");\nINSERT INTO `%(table)s` VALUES (" % {
"table": table_name,
} + split
line = line.replace(split, fixed_split)
line = line.replace(",();\n", ";\n")
wh.write(line)
for name, field in keys:
wh.write('CREATE INDEX "%(table)s_%(key)s" ON "%(table)s" (`%(field)s`);\n' % {
"table": table_name,
"key": name,
"field": field,
})
wh.write("END TRANSACTION;\n")
wh.close()

The MIT License (MIT)

Copyright (c) 2014 Kevin Brown

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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