Project

General

Profile

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

Behrang Noroozinia, 2011-07-04 06:26

 
1
#! /usr/bin/env python
2

    
3
import sys
4

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

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

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