for循环在函数中不存在

2024-10-03 13:23:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我在将一些工作代码转换为函数时遇到了一些问题。第一个代码示例运行正常,但下面的函数挂起。使用print进行的一点调试表明,该函数一直通过光标移动到最终记录,并将其附加到列表中,但程序挂起并且不会退出。你知道吗

游标是cx\ U Oracle模块的一部分。其目的是查询oracledb,然后创建一个列表。我在几个查询上测试了原始代码,没有问题(最大返回大约15000行)。在这一点上,我可以使代码使用原始格式工作,但我想知道我可能在函数中做错了什么。你知道吗

工作代码:

cursor = db.cursor()
cursor.execute(mysqlexp)
for row in cursor:
    myList.append(row)
cursor.close()

功能(不工作):

def sqlToList(listname, sqlexp):
    cursor = db.cursor()
    cursor.execute(sqlexp)
    for row in cursor:
        listname.append(row)
        #print statement here indicates that final record appends
        #but then the program stops responding
    #print statement here never appears (indicating for loop hasn't exited?)
    cursor.close()
sqlToList(myList, mysqlexp)

Tags: 函数代码in列表forcloseexecutedb