4 usage: %(progname)s [db filenames...] 8 import os, sys, string, time, getopt
12 tfn = os.path.join(path, tableName +
'.sql')
13 warn(
"dumpTables", tfn)
14 cmd =
"sqlite3 %s '.schema %s' > %s" % (fn, tableName, tfn)
22 conn = sqlite3.connect(fn)
28 fieldList.append(
"%s > ''" % field)
30 c.execute(
"SELECT %s FROM %s WHERE %s" % (string.join(fields,
","), tableName, string.join(fieldList,
" AND ")))
43 fieldList.append(
"%s='%s'" % (field, row[i]))
46 open(
".currentRow",
"w").write(
"%s\n" % string.join(fieldList))
48 c.execute(
"select * from %s where %s" % (tableName, string.join(fieldList,
" AND ")))
50 warn(
"Error with", row)
58 if type(col) == type(
None):
60 elif type(col) == type(1):
61 parts.append(str(col))
63 if c.rs.col_defs[i][0] ==
"odb_value": col =
"" 64 col = string.replace(col,
"'",
"''")
65 parts.append(
"'" + col +
"'")
67 sql =
"INSERT INTO %s VALUES (%s);" % (tableName, string.join(parts,
", "))
77 tfn = os.path.join(path, tableName +
'.sql')
78 warn(
"dumpTables", tfn)
79 cmd =
"sqlite3 %s '.dump %s' > %s" % (fn, tableName, tfn)
88 warn(
"loadTables", tableName)
90 tfn = os.path.join(path, tableName +
'.sql')
92 cmd =
"sqlite3 %s.new < %s" % (fn, tfn)
102 if not os.path.exists(fn):
return 104 path, f = os.path.split(fn)
106 dumpPath = os.path.join(path,
"dump")
107 try: os.mkdir(dumpPath, 0700)
108 except os.error, reason:
pass 110 cmd =
"sqlite3 %s .tables" % fn
111 fp = os.popen(cmd,
"r") 115 tables = body.split() 120 for tableName
in tables:
121 if tableName ==
"mb_msgdata":
122 ret =
dumpTable2(dumpPath, fn, tableName, fields=(
'mnum', ))
123 elif tableName ==
"mb_thread":
124 ret =
dumpTable2(dumpPath, fn, tableName, fields=(
'thr_id',
'mnum'))
127 if ret
is False:
return 129 try: os.unlink(fn +
".new")
130 except os.error, reason:
pass 132 for tableName
in tables:
134 if ret
is False:
return 136 now = int(time.time())
137 dateStr = time.strftime(
"%Y%m%d_%H%M%S", time.localtime(now))
140 os.rename(fn, fn +
"_" + dateStr)
141 os.rename(fn +
".new", fn)
148 print __doc__ % vars()
150 def main(argv, stdout, environ):
152 optlist, args = getopt.getopt(argv[1:],
"", [
"help",
"test",
"debug"])
158 for (field, val)
in optlist:
159 if field ==
"--help":
162 elif field ==
"--debug":
164 elif field ==
"--test":
176 if __name__ ==
"__main__":
177 main(sys.argv, sys.stdout, os.environ)
def loadTable(path, fn, tableName)
def dumpTable(path, fn, tableName)
def dumpTable2(path, fn, tableName, fields)
def main(argv, stdout, environ)