问题描述
我尝试使用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()中列出,也未找到驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!