即使在 pyodbc.dataSources() 中列出,也未找到驱动程序 [英] Driver not found even though it's listed in pyodbc.dataSources()
问题描述
我尝试使用 pyodbc 访问 MS Access .mdb 数据库.我已经设置了 ODBC 驱动程序,该驱动程序已注册到我的 DSN,我可以在 pyodbc.dataSources()
I tried to access MS Access .mdb database using pyodbc. I've set up the ODBC driver, The driver has been registered to my DSN and I can find in the pyodbc.dataSources()
sources = pyodbc.dataSources()
dsns = list(sources.keys())
dsns.sort()
sl = []
for dsn in dsns:
sl.append('%s [%s]' % (dsn, sources[dsn]))
print('\n'.join(sl))
结果:
Excel Files [Microsoft Excel Driver (*.xls)]
MS Access DB [Microsoft Access Driver (*.mdb, *.accdb)]
MS Access Database [Microsoft Access Driver (*.mdb)]
dBASE Files [Microsoft dBase Driver (*.dbf)]
因此,我尝试使用以下命令访问数据库:
So, I tried to access the DB by using this command:
con = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=H:\access-panda\data\myDB.mdb;')
但是,我不断收到此错误:
But, I keep getting this error:
Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
我试图解决这个问题..但没有运气.我错过了什么吗?
I've tried to resolved the issue.. but no luck. Did I miss something?
推荐答案
pyodbc.dataSources()
生成的列表具有误导性,因为它显示了 32 位和 64 位的结果平台".在装有 32 位 Office 的机器上,在 64 位 Python 下运行代码将生成列表
The list produced by pyodbc.dataSources()
is misleading because it shows the results from both the 32-bit and 64-bit "Platform". On a machine with 32-bit Office, running your code under 64-bit Python will produce the list
Excel Files [Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)]
MS Access Database [Microsoft Access Driver (*.mdb, *.accdb)]
但是如果我们打开 64 位 ODBC 管理器,我们会看到它们都是针对 32 位平台"的
but if we open the 64-bit ODBC Administrator we see that they are both for the 32-bit "Platform"
并且相关的驱动程序将不可用于在 64 位 Python 下运行的 pyodbc.
and the associated drivers will not be available to pyodbc running under 64-bit Python.
获取可用驱动程序列表的更可靠方法是使用
A more reliable way to get the list of available drivers is to use
drivers = pyodbc.drivers()
print(drivers)
那应该只显示可用于您的特定平台"(即 64 位或 32 位 Python)的驱动程序.
That should only show you the drivers that are available to your particular "Platform" (i.e., 64-bit or 32-bit Python).
这篇关于即使在 pyodbc.dataSources() 中列出,也未找到驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!