使用 pyodbc 连接到 MS Access 2007 (.accdb) 数据库 [英] Connecting to MS Access 2007 (.accdb) database using pyodbc

查看:52
本文介绍了使用 pyodbc 连接到 MS Access 2007 (.accdb) 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Win7 x64 上,使用 Python 2.7.1 x64.我正在将我在 VC++ 中创建的应用程序移植到 Python 以用于教育目的.
原始应用程序使用以下连接字符串连接到 MS Access 2007 格式的 DB 文件没有问题:
OleDbConnection^ conn = gcnew OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|DB.accdb");
现在,当我尝试使用 pyodbc 和以下连接字符串在 Python 中连接到同一个 DB 文件(这次放在 C: 中)时:
conn = pyodbc.connect("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:DB.accdb;")
,无论我是保留 OLEDB 提供程序还是使用 Provider=MSDASQL; 如前所述 此处(MS 提到它不可用对于 64 位),我不断收到以下错误:

I am on Win7 x64, using Python 2.7.1 x64. I am porting an application I created in VC++ to Python for educational purpouses.
The original application has no problem connecting to the MS Access 2007 format DB file by using the following connection string:
OleDbConnection^ conn = gcnew OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|DB.accdb");
Now, when I try to connect to the same DB file (put in C: this time) in Python using pyodbc and the following conenction string:
conn = pyodbc.connect("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:DB.accdb;")
, and no matter whether I keep the OLEDB provider or I use the Provider=MSDASQL; as mentioned here (MS mentions it's not availiable for 64bit), I keep getting the following error:

pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] 未找到数据源名称且未指定默认驱动程序 (0) (SQLDriverConnectW)')

可能导致此问题的原因是什么?

What might cause this problem?

添加:我更仔细地研究了 pyodbc 文档并尝试了 conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=c:\DB.accdb;") - 同样的错误.这真的很奇怪,因为 pyodbc.dataSources() 显示我有这个提供者.

ADD: I have looked into pyodbc docs more closely and tried conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=c:\DB.accdb;") - the same error. This is really weird, since the pyodbc.dataSources() shows that I have this provider.

添加 2:我尝试了 win32com.client 用法,例如 here 以便使用连接OLE DB - 没有成功.似乎不可能,什么都行不通.

ADD2: I tried win32com.client usage such as here in order to connect by using OLE DB - no success. Seems that it's impossible, nothing works.

推荐答案

  1. 尝试使用类似下面的内容,而不是使用与 OLeDb 相同的字符串:
    "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:DB.accdb;"

您可能无法直接从 x64 Python 应用程序与驱动程序对话:Access 2007 及其 ACE 驱动程序仅为 32 位.
而是获取 Access 2010 的 ACE x64 驱动程序,但请注意,如果您已经安装了 Access 或 ACE 驱动程序 32 位,它将无法工作.
如果您希望您的应用程序在其他系统上运行,我会坚持使用 32 位版本的 Python 和 ACE 驱动程序:不建议混合使用 x64 和 x86 版本的 Office 工具和驱动程序,您最终可能会得到很多如果你这样做,问题就会出现.

You may not be able to talk to the driver directly from your x64 Python application: Access 2007 and its ACE driver are 32 bits only.
Instead, get the ACE x64 driver for Access 2010, but be careful that if you already have Access or the ACE driver 32bit installed, it won't work.
I would stick to the 32bit versions of Python and of the ACE driver if you expect your app to be run on other systems: it is not recommended to mix x64 and x86 versions of Office tools and drivers, you'll probably end up with lots of issues if you do.

如果问题不在于 32/64 位混合,那么也许 这个问题有你想要的答案.

If the issue is not with the 32/64bit mix, then maybe this question has the answer you seek.

这篇关于使用 pyodbc 连接到 MS Access 2007 (.accdb) 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆