2
# -*- coding: UTF-8 -*-
4
Generates a data-only sqlite dump, with insert statements including column
7
@author: Aurélien Gâteau <aurelien.gateau@free.fr>
16
# Seems to be the Python 2.6 way to get sqlite
17
from sqlite3 import dbapi2 as sqlite
19
from pysqlite2 import dbapi2 as sqlite
24
cursor.execute("select name from sqlite_master where type='table'")
25
return [x[0] for x in cursor.fetchall()]
28
def getTableColumnList(cx, table):
30
cursor.execute("select * from %s" % table)
31
return [x[0] for x in cursor.description]
34
def dumpTable(cx, dbFileName, table, fl):
35
rx = re.compile(u"^insert into %s values" % table, re.IGNORECASE)
37
columnList = getTableColumnList(cx, table)
38
newText = u"insert into %s(%s) values" % (table, ",".join(columnList))
40
child = subprocess.Popen(["sqlite3", dbFileName], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
41
child.stdin.write(".mode insert %s\nselect * from %s;\n" % (table, table))
44
for line in child.stdout.readlines():
45
line = unicode(line, "utf-8")
46
line = rx.sub(newText, line)
47
fl.write(line.encode("utf-8"))
50
def dumpDatabase(dbFileName, dumpFile):
51
cx = sqlite.connect(os.path.abspath(dbFileName))
52
for table in getTableList(cx):
53
dumpTable(cx, dbFileName, table, dumpFile)
57
dbFileName = sys.argv[1]
58
dumpFileName = sys.argv[2]
59
dumpFile = file(dumpFileName, "w")
60
dumpDatabase(dbFileName, dumpFile)
63
if __name__ == "__main__":