Project

General

Profile

RE: Migrating from sqlite3 to mysql ยป sqlite3-to-mysql.py

Behrang Noroozinia, 2011-07-05 14:29

 
1
#! /usr/bin/env python
2

    
3
import sys
4

    
5
def main():
6
    print "SET sql_mode='NO_BACKSLASH_ESCAPES';"
7
    lines = sys.stdin.read().splitlines()
8
    for line in lines:
9
        processLine(line)
10

    
11
def processLine(line):
12
    if (
13
        line.startswith("PRAGMA") or 
14
        line.startswith("BEGIN TRANSACTION;") or
15
        line.startswith("COMMIT;") or
16
        line.startswith("DELETE FROM sqlite_sequence;") or
17
        line.startswith("INSERT INTO \"sqlite_sequence\"")
18
       ):
19
        return
20
    line = line.replace("AUTOINCREMENT", "AUTO_INCREMENT")
21
    line = line.replace("DEFAULT 't'", "DEFAULT '1'")
22
    line = line.replace("DEFAULT 'f'", "DEFAULT '0'")
23
    line = line.replace(",'t'", ",'1'")
24
    line = line.replace(",'f'", ",'0'")
25
    in_string = False
26
    newLine = ''
27
    for c in line:
28
        if not in_string:
29
            if c == "'":
30
                in_string = True
31
            elif c == '"':
32
                newLine = newLine + '`'
33
                continue
34
        elif c == "'":
35
            in_string = False
36
        newLine = newLine + c
37
    print newLine
38

    
39
if __name__ == "__main__":
40
    main()
    (1-1/1)