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.
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.). 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.
It doesn't error - the print() statements work, but the loop just stops after the second print statement. If I comment the second print() out, the first print will return four results.
Cursor.commit()
doesn't change the behavior.
# 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