简体   繁体   English

使用pyodbc连接到MS Access 2007(.accdb)数据库

[英]Connecting to MS Access 2007 (.accdb) database using pyodbc

I am on Win7 x64, using Python 2.7.1 x64. 我在Win7 x64上,使用Python 2.7.1 x64。 I am porting an application I created in VC++ to Python for educational purpouses. 我正在将我在VC ++中创建的应用程序移植到Python中,用于教育目的。
The original application has no problem connecting to the MS Access 2007 format DB file by using the following connection string: 使用以下连接字符串连接到MS Access 2007格式DB文件时,原始应用程序没有问题:
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: 现在,当我尝试使用pyodbc和以下连接字符串连接到Python中的相同DB文件(放在C:\\这次)时:
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; ,无论我是保留OLEDB提供者还是使用Provider=MSDASQL; as mentioned here ( MS mentions it's not availiable for 64bit ), I keep getting the following error: 正如这里 提到的MS提到它不适用于64位 ),我不断收到以下错误:

pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnectW)')

What might cause this problem? 什么可能导致这个问题?

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. ADD:我更仔细地研究了pyodbc文档并尝试了conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=c:\\\\DB.accdb;") -同样的错误。 This is really weird, since the pyodbc.dataSources() shows that I have this provider. 这真的很奇怪,因为pyodbc.dataSources()表明我有这个提供者。

ADD2: I tried win32com.client usage such as here in order to connect by using OLE DB - no success. ADD2:我想win32com.client使用如这里为了使用OLE DB连接-没有成功。 Seems that it's impossible, nothing works. 似乎这是不可能的,没有任何作用。

  1. Try to use something like the following instead of using the same string as the one for OLeDb: 尝试使用类似下面的内容而不是使用与OLeDb相同的字符串:
    "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\DB.accdb;"

  2. 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. 您可能无法直接从x64 Python应用程序与驱动程序通信:Access 2007及其ACE驱动程序仅为32位。
    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. 相反,获取Access 2010ACE x64驱动程序 ,但要小心,如果您已经安装了Access或ACE驱动程序32位,它将无法正常工作。
    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位版本的Python和ACE驱动程序:不建议混合x64和x86版本的Office工具和驱动程序,你可能最终会有很多如果你这样做的问题。

  3. If the issue is not with the 32/64bit mix, then maybe this question has the answer you seek . 如果问题不在于32/64位混音,那么也许这个问题有你想要的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM