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

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

问题描述

我在Win7的X64,使用Python 2.7.1 64位。我端起我在VC ++创建Python的教育效果影响不大的应用程序。
原来的应用程序使用下面的连接字符串没有问题连接到的MS Access 2007格式数据库文件:
OleDbConnection的^康恩= gcnew的OleDbConnection(供应商= Microsoft.ACE.OLEDB.12.0;数据源= | DataDirectory目录| DB.accdb);
使用pyodbc及以下conenction字符串中的Python:现在,当我尝试连接到同一个数据库文件(\这个时候放C):
康恩= pyodbc.connect(DRIVER = {Microsoft Access驱动程序(* .MDB,* .ACCDB)};提供商= Microsoft.ACE.OLEDB.12.0;数据源= C:\ DB.accdb; )
,也不管我是否保留OLEDB提供商或我使用了提供程序= MSDASQL; 中提到的此处 MS提到它不是availiable对于64位),我不断收到以下错误:

pyodbc.Error:('IM002','[IM002] [微软] [ODBC驱动程序管理器]数据源名称找不到和未指定默认驱动程序(0)(SQLDriverConnectW)')

什么可能导致这个问题?

地址: 我更加仔细地看着pyodbc文档,并试图康恩= pyodbc.connect(驱动程序= {Microsoft Access驱动程序(* .MDB,* .ACCDB)}; DBQ = C:\\ DB.accdb; ) - 同样的错误。这是很奇怪的,因为pyodbc.dataSources()显示,我有这样的提供者。

ADD2: 我试图win32com.client使用,比如这里,以便通过使用OLE DB连接 - 没有成功。看来,这是不可能的,没有什么工作。

解决方案
  1. 尽量使用像使用相同字符串作为一个用于OLEDB以下代替:
    驱动程序= {Microsoft Access驱动程序(* .MDB,* .ACCDB)}; DBQ = C:\ DB.accdb;

  2. 您可能无法从您的x64 Python应用程序交谈的驱动程序直接:访问2007及ACE驾驶员是32位的唯一
    相反,获得进入2010 的 ACE 64驱动程序,但要小心如果你已经有访问或安装了ACE驱动32位,这是行不通的。照片 我会坚持到Python,如果你希望你的应用程序要在其他系统上运行的ACE驱动程序的32位版本:它不建议混合x64和x86的办公工具和驱动程序的版本,你可能最终与很多的问题,如果你这样做。

  3. 如果该问题不与32位/ 64位混合的话,说不定<一个href="http://stackoverflow.com/questions/3251702/how-to-connect-pyodbc-to-an-access-mdb-database-file">this问题有你寻求的答案。

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] 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. This is really weird, since the pyodbc.dataSources() shows that I have this provider.

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. Try to use something like the following instead of using the same string as the one for 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.
    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.

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

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

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