I found this Perl script while migrating my SQLite database to mysql
I was wondering (since I don\'t know Perl) how could one rewrite this in Python?
Bonus point
Alex Martelli's solution above works good, but needs some fixes and additions:
In the lines using regular expression substitution, the insertion of the matched groups must be double-escaped OR the replacement string must be prefixed with r to mark is as regular expression:
line = re.sub(r"([^'])'t'(.)", "\\1THIS_IS_TRUE\\2", line)
or
line = re.sub(r"([^'])'f'(.)", r"\1THIS_IS_FALSE\2", line)
Also, this line should be added before print:
line = line.replace('AUTOINCREMENT', 'AUTO_INCREMENT')
Last, the column names in create statements should be backticks in MySQL. Add this in line 15:
sub = sub.replace('"','`')
Here's the complete script with modifications:
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()
sub = sub.replace('"','`')
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'(.)", "\\1THIS_IS_TRUE\\2", line)
line = line.replace('THIS_IS_TRUE', '1')
line = re.sub(r"([^'])'f'(.)", "\\1THIS_IS_FALSE\\2", line)
line = line.replace('THIS_IS_FALSE', '0')
line = line.replace('AUTOINCREMENT', 'AUTO_INCREMENT')
if re.search('^CREATE INDEX', line):
line = line.replace('"','`')
print line,
main()