PYODBC InterfaceError-找不到数据源名称 [英] PYODBC InterfaceError- Data source name not found
问题描述
我正在尝试使用pyodbc将Python连接到MS Access数据库,但是每次遇到以下错误时:
I am trying to connect Python to MS Access Database using pyodbc but every time I get the following error:
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
这是我写的将python连接到MS Access的内容:
And this what I have written to connect python to MS Access:
import pyodbc
conn = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\PILOT_DATA.accdb;')
cursor = conn.cursor()
cursor.execute('select * from p_inventor')
for row in cursor.fetchall():
print (row)
根据错误,找不到数据源名称,因此我将"DRIVER"更改为"DSN"
According to the error, it doesn't find the Data source name and so I changed the 'DRIVER' to 'DSN'
import pyodbc
conn = pyodbc.connect(r'DSN={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\PILOT_DATA.accdb;')
cursor = conn.cursor()
cursor.execute('select * from p_inventor')
for row in cursor.fetchall():
print (row)
但这没有帮助.我收到以下错误:
But it doesn't help. I get the following error:
pyodbc.Error: ('IM010', '[IM010] [Microsoft][ODBC Driver Manager] Data source name too long (0) (SQLDriverConnect)')
我尝试过的其他解决方法是同时使用python 32位和64位
Other workaround I have tried is to use both python 32 and 64 bit
以下是版本详细信息:
- Python 3.7.4 64位
- 点19.2.3
- pyodbc-4.0.27
- Office365 16
了解将Python连接到ACCESS数据库还能做些什么,将非常有帮助. 在此先感谢!
Would be really helpful to know what else I can do to connect Python to ACCESS database. Thanks in Advance!
推荐答案
我已经通过安装Access数据库引擎解决了此问题.为此,我必须取消安装office365程序->安装访问数据库引擎->重新安装office365.然后代码可以完美运行!
I have solved this issue by installing the Access Database Engine. In order to do that, I had to unistall the office365 program-> install access database engine-> re-install office365. And then the code runs perfectly!
这篇关于PYODBC InterfaceError-找不到数据源名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!