很抱歉,我有一个sqlite文件,它包含很多表(比如table_A到table_Z) 对不起,我只能使用c.execute(' SELECT ST_Name FROM table_A')并再次执行table_B。
我如何使用循环来完成它,我已经整天搜索,但我没有得到答案,请帮帮我。
THX !!
关于我的代码,请参阅下面的
import sqlite3
import numpy as np
Sqlite_Path = 'D:\Student.sqlite'
conn = sqlite3.connect(Sqlite_Path)
c = conn.cursor()
c.execute('SELECT ST_Name FROM Table_A')
data = c.fetchall()
# do something
c.execute('SELECT ST_Name FROM Table_B')
data = c.fetchall()
# do something again
答案 0 :(得分:0)
这看起来像是我的作业,但无论如何:
tables = ["Table_A ", "Table_B", "Table_C"]
for table in tables:
c.execute('SELECT ST_Name FROM {}'.format(table))
data = c.fetchall()
# do something
答案 1 :(得分:0)
如果您不了解所有表格或出于其他原因,您可以执行以下操作:
tables = [r[0] for r in db.execute('select name from sqlite_master where name like "table_%" and type = "table"')]
for table in tables:
stmt = 'SELECT * FROM {};'.format(table)
c = db.execute(stmt)
rows = c.fetch_all()
# ... do something with results
这会在sqlitefile
中返回所有表名