python - 获取变量错误 - Mytable 和 tableName 可以在此 Python 代码中未定义。如何纠正?
问题描述
我越来越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)
解决方案
推荐阅读
- jenkins - 使用 Jenkins 作业在 bitbucket 中创建拉取请求
- json - Ansible 获取标准输出键值
- java - ANTLR v4:如何在侦听器中实现规则替代的停用,例如语法文件中的嵌入式语义谓词?
- json - 找不到模块 json
- python - 如何配置 chronograf 以在特定端口中运行?
- symfony - Symfony:角色管理上的“数组到字符串转换”错误
- graphql - 包含 ALL 的 graphQL 过滤器数组
- c++ - 我应该使用高分辨率时间类型作为参数来避免 duration_casts 吗?
- java - android:背景不适用于 Theme.MaterialComponents
- laravel - laravel 1071 指定的密钥太长;最大密钥长度为 1000 字节