1

地狱家伙刚刚跳入python,我很难弄清楚这一点

我有 2 个查询。. query1现在query2我怎么能说
row = cursor.fetchone()我指的是query1而不是query2

cursor = conn.cursor()
query1  = cursor.execute("select * FROM spam")
query2  = cursor.execute("select * FROM eggs")
row = cursor.fetchone ()

多谢你们

4

3 回答 3

3

执行第二个查询后,第一个查询的结果就消失了。(的返回值execute没有用。)同时处理两个查询的正确方法是有两个游标:

cursor1 = conn.cursor()
cursor2 = conn.cursor()
cursor1.execute("select * FROM spam")
cursor2.execute("select * FROM eggs")
cursor1.fetchone() #first result from query 1
cursor2.fetchone() #first result from query 2
于 2012-04-30T05:09:00.107 回答
1

它没有。from 的返回值cursor.execute是没有意义的。根据PEP 249

.execute(operation[,parameters]) 

        Prepare and execute a database operation (query or
        command)...

        [...]

        Return values are not defined.
于 2012-04-30T05:03:30.977 回答
1

你不能按照你想要的方式去做。改为执行以下操作:

cursor = conn.cursor()
cursor.execute("select * FROM spam")
results1 = cursor.fetchall()
cursor.execute("select * FROM eggs")

if results1 is not None and len(results1) > 0:
    print "First row from query1: ", results1[0]

row = cursor.fetchone()
if row is not None:
    print "First row from query2: ", row
于 2012-04-30T05:03:36.600 回答