如何解决获取变量错误-在此Python代码中无法定义Mytable和tableName该如何纠正?
我得到Variable Error - Mytable and tableName can be undefined in this Python Code
。如何纠正?
import pyodbc
# we may be interested in finding all the drivers we have access to
for driver in pyodbc.drivers():
# print the driver name
# print(driver)
# if the driver name has '.xlsx' in it we found it!
if '.xlsx' in driver:
myDriver = driver
# define our connection string
conn_str = (r'DRIVER={' + myDriver + '};'
r'DBQ="C:\Python_Project\Tap Report for 26-Aug-2022.xlsx;'
r'ReadOnly=1') # ReadOnly set to 0 means we can edit the data.
# define our connection,autocommit MUST BE SET TO TRUE,also we can edit data.
cnxn = pyodbc.connect(conn_str,autocommit=True)
crsr = cnxn.cursor()
# loop through all the tables
for worksheet in crsr.tables():
# display the worksheet
print(worksheet)
# grab the table name.
tableName = worksheet[2]
# define our query to grab the data.
# we want this "SELECT Topic FROM [Sheet1$]"
crsr.execute("SELECT Topic FROM [{}]".format(tableName))
# print each row of data.
for row in crsr:
print(row.Topic)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。