简体   繁体   English

第一个 Cursor.execute() 后循环停止

[英]Loop stops after first Cursor.execute()

Using pyodbc, I'm trying to loop through sheets in Excel, read ones that start with the string "Appointment", then write the results to an existing table in an Access DB.使用 pyodbc,我试图遍历 Excel 中的工作表,读取以字符串“Appointment”开头的工作表,然后将结果写入 Access DB 中的现有表中。

I'm able to loop through all the sheets and identify the ones that start with "Appointment" (there's four of them - Appointment1, Appointment2, etc.).我能够遍历所有工作表并识别以“约会”开头的工作表(其中有四个 - Appointment1、Appointment2 等)。 I can also read the data on any one of the sheets found, and I can write the results to the DB table.我还可以读取找到的任何一张表上的数据,并将结果写入数据库表。 When I try to do all that in a for loop, I'm able to get all the sheet names, but as soon as I execute() a select statement, the loop stops.当我尝试在 for 循环中执行所有这些操作时,我可以获得所有工作表名称,但是一旦我执行() select 语句,循环就会停止。

It doesn't error - the print() statements work, but the loop just stops after the second print statement.它不会出错 - print() 语句有效,但循环在第二个 print 语句后停止。 If I comment the second print() out, the first print will return four results.如果我将第二个 print() 注释掉,第一个 print 将返回四个结果。

Cursor.commit() doesn't change the behavior. Cursor.commit()不会改变行为。

# execute select stmt
def select_xls_data(tbl_name):
    select_stmt_XLS = 'SELECT * from [' + tbl_name + ']'
    result_XLS = crsr_XLS.execute(select_stmt_XLS).fetchall()
    return result_XLS

# loop through XLS sheets
for table_info in crsr_XLS.tables():
    tbl_name = table_info.table_name
    if (tbl_name.startswith('Appointment')):
        print(tbl_name)  # will succesfully loop through all sheets
        print(select_xls_data(tbl_name))  # will only loop once

It seems you have incorrect syntax in the select statement, which should be:看来您在 select 语句中的语法不正确,应该是:

select_stmt_XLS = 'SELECT * from ' + tbl_name + ''

I still don't know why the loop was stopping in my original question, but I rewrote the code to achieve my goal.我仍然不知道为什么循环在我原来的问题中停止,但我重写了代码以实现我的目标。

The new code does:新代码可以:

  1. Loop through all the tables in the file and add the tables of interest to a list循环遍历文件中的所有表并将感兴趣的表添加到列表中
  2. Loop through the list created and execute a select statement for each table of interest遍历创建的列表并为每个感兴趣的表执行 select 语句
  3. Add the results of each select to a results list将每个 select 的结果添加到结果列表中
def select_xls_data(tbl_name):
    select_stmt_XLS = 'SELECT * from ' + tbl_name
    result_XLS = crsr_XLS.execute(select_stmt_XLS).fetchall()
    return result_XLS

# discover the tables of interest and write their names to a list
tables = []
for table_info in crsr_XLS.tables():
    tbl_name = table_info.table_name
    if (tbl_name.startswith('Appointment')): tables.append('['+tbl_name+']')

# loop through tables of interest, execute select stmt on each and append each result set to a list
results = []
for tbl_name in tables: results.append(select_xls_data(tbl_name))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM