这里是代码:
import sqlite3
cx = sqlite3.connect("test.db")
cu = cx.cursor()
# create table 1.
cu.execute("create table user_info1 (user_id text primary key,followers integer, asks integer, answers integer, goods integer)")
# create table 2
cu.execute("create table user_info2 (user_id text primary key,followers integer, asks integer, answers integer, goods integer)")
现在我想知道test.db中有多少个表:
cu.execute('.tables') # doesn't work.
cu.execute('select * from test.sqlite_master where type = "table"')
# OperationalError: no such table: test.sqlite_master
上述方法对我不起作用。有什么提示吗?谢谢!
答案 0 :(得分:2)
您的数据库名称不是test
(这将是文件名称的一部分),但是main
:
cu.execute("SELECT * FROM main.sqlite_master WHERE type = 'table'")
main
是默认的数据库名称,因此您甚至不需要指定它:
cu.execute("SELECT * FROM sqlite_master WHERE type = 'table'")