import sqlite3
con = sqlite3.connect('/path/to/file.pf')
cur = con.cursor()
cur.execute('select something from table1') #1st select
cur.execute('select something_else from table2') #2nd select
rows = cur.fetchall()
for r in rows:
print r
我只从第一个SELECT
获得结果。
如何包含第二个SELECT
中的行?
答案 0 :(得分:2)
使用两个单独的游标,或创建UNION
选择以生成一个结果。
两个单独的游标可以与itertools.chain()
链接在一起:
from itertools import chain
cur1 = con.cursor()
cur1.execute('select something from table1') #1st select
cur2 = con.cursor()
cur2.execute('select something_else from table2') #2nd select
for row in chain(cur1, cur2):
print row
请注意,您无需致电cursor.fetchall()
;迭代可以直接在行上完成。
UNION
选择要求您的选择返回相同数量的列:
cur = con.cursor()
cur.execut('''\
select something from table1 -- 1st select
union
select something_else from table2 -- 2nd select
''')
for row in cur:
print row