2010年7月6日星期二

python 读取SQLite数据库所有表的定义(原创)

from sqlite3 import dbapi2 as sqlite
import re
p=re.compile("\(u(\'\w+\'),\)")

con = sqlite.connect("trac.db")

cur = con.cursor()
cur.execute("select name from sqlite_master where type='table'")
ticol = cur.fetchall()
for i in range (0,len(ticol)):
sqlstr=' '.join(["select * from sqlite_master where type='table'","and name =", p.match(str(ticol[i])).group(1)])

cur.execute(sqlstr)
stu = cur.fetchone()





f = file("table.txt","a")
f.write(str(i+1))
f.write( ': ')
for j in range(0,len(stu)):



f.write(str(stu[j]))
f.write('\n')
else:
f.write('-------------------------------------\n')
f.close()

cur.close()

没有评论:

发表评论