Skip to content

Instantly share code, notes, and snippets.

@gerhard-tinned
Forked from yanbab/sqlite2mysql.py
Last active August 30, 2015 19:42
Show Gist options
  • Save gerhard-tinned/76736287677e0d9948f9 to your computer and use it in GitHub Desktop.
Save gerhard-tinned/76736287677e0d9948f9 to your computer and use it in GitHub Desktop.
Convert SQLite database toMySQL/MariaDB
#! /usr/bin/env python
#
# Usage:
#
# 1. Dump your sqlite db : sqlite3 database.sqlite3 .dump > dump.sql
# 2. run script : cat dump.sql | python sqlite2mysql.py > dump.my.sql
# 3. import SQL schema in MySQL : mysql -u USER -p DBName < dump.my.sql
#
import sys
def main():
print "SET sql_mode='NO_BACKSLASH_ESCAPES';"
lines = sys.stdin.read().splitlines()
for line in lines:
processLine(line)
def processLine(line):
if (
line.startswith("PRAGMA") or
line.startswith("BEGIN TRANSACTION;") or
line.startswith("COMMIT;") or
line.startswith("DELETE FROM sqlite_sequence;") or
line.startswith("INSERT INTO \"sqlite_sequence\"")
):
return
line = line.replace("AUTOINCREMENT", "AUTO_INCREMENT")
line = line.replace("DEFAULT 't'", "DEFAULT '1'")
line = line.replace("DEFAULT 'f'", "DEFAULT '0'")
line = line.replace(",'t'", ",'1'")
line = line.replace(",'f'", ",'0'")
in_string = False
newLine = ''
for c in line:
if not in_string:
if c == "'":
in_string = True
elif c == '"':
newLine = newLine + '`'
continue
elif c == "'":
in_string = False
newLine = newLine + c
print newLine
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment