Python 如何打印两个不同的选择结果

Python 如何打印两个不同的选择结果,python,python-2.7,sqlite,Python,Python 2.7,Sqlite,我只从第一次选择中获得结果 如何也包括第二个选择中的行?使用两个单独的游标,或创建一个联合选择以生成一个结果 两个单独的光标可以链接在一起: 请注意,您不必调用cursor.fetchall();可以直接对行进行迭代 UNIONselect要求您的select返回相同数量的列: from itertools import chain cur1 = con.cursor() cur1.execute('select something from table1') #1st select cur2

我只从第一次
选择中获得结果


如何也包括第二个
选择
中的行?

使用两个单独的游标,或创建一个
联合
选择以生成一个结果

两个单独的光标可以链接在一起:

请注意,您不必调用
cursor.fetchall()
;可以直接对行进行迭代

UNION
select要求您的select返回相同数量的列:

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
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
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