MySQL Dump to SQLite Import


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import re, fileinput
  2.  
  3. def main():
  4. for line in fileinput.input():
  5. process = False
  6. for nope in ('BEGIN TRANSACTION','COMMIT',
  7. 'sqlite_sequence','CREATE UNIQUE INDEX'):
  8. if nope in line: break
  9. else:
  10. process = True
  11. if not process: continue
  12. m = re.search('CREATE TABLE "([a-z_]*)"(.*)', line)
  13. if m:
  14. name, sub = m.groups()
  15. line = '''DROP TABLE IF EXISTS %(name)s;
  16. CREATE TABLE IF NOT EXISTS %(name)s%(sub)s
  17. '''
  18. line = line % dict(name=name, sub=sub)
  19. else:
  20. m = re.search('INSERT INTO "([a-z_]*)"(.*)', line)
  21. if m:
  22. line = 'INSERT INTO %s%s\n' % m.groups()
  23. line = line.replace('"', r'\"')
  24. line = line.replace('"', "'")
  25. line = re.sub(r"([^'])'t'(.)", "\1THIS_IS_TRUE\2", line)
  26. line = line.replace('THIS_IS_TRUE', '1')
  27. line = re.sub(r"([^'])'f'(.)", "\1THIS_IS_FALSE\2", line)
  28. line = line.replace('THIS_IS_FALSE', '0')
  29. print line,
  30.  
  31. main()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.