如何使用pyodbc在嵌套循环中调用不同的查询

时间:2019-11-30 07:04:27

标签: python ssms pyodbc

我有类似

cursor.execute("select * from table1") # table1 has 10 rows
for k,row in enumerate(cursor,1):
    if row[3] != None:
        cursor.execute("select * from table2") # table2 has 10 rows
        for row2 in cursor():
            print(row2)
    print(row)

SQL不允许在同一个come连接内使用多个游标。那我该如何从table2中获取数据

1 个答案:

答案 0 :(得分:0)

听起来好像您应该在两个表之间进行联接:

def ask_ok(prompt, retries=4, reminder='Please try again!'):
    while True:
        ok = input(prompt)
        if ok in ('y', 'ye', 'yes'):
            return True
        if ok in ('n', 'no', 'nop', 'nope'):
            return False
        retries = retries - 1
        if retries < 0:
            raise ValueError('invalid user response')
        print(reminder)

您发现Python语法不足以满足您的要求的原因是,一个表与另一个表的关系是要从数据库而不是在Python中处理的。