Pyodbc和MS Access 2010连接错误 [英] pyodbc and ms access 2010 connection error

查看:71
本文介绍了Pyodbc和MS Access 2010连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用pyodbc访问Microsoft Access 2010数据库(accdb)? 以前,我使用了mdb数据库,该数据库在连接字符串为

How can I access my Microsoft Access 2010 database (accdb) with pyodbc? Before, I used an mdb Database, which worked fine with the connection string being:

ODBC_CONN_STR = 'DRIVER={Microsoft Access Driver (*.mdb)};DBQ=%s;' % ACCESS_DATABASE_FILE 

现在我使用:

import pyodbc
ACCESS_DATABASE_FILE = "PSA_TEST.accdb"
ODBC_CONN_STR = 'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;' % ACCESS_DATABASE_FILE
conn = pyodbc.connect(ODBC_CONN_STR)

我得到的错误是: pyodbc.Error:('HY000','[HY000] [Microsoft] [ODBC-TreiberfürMicrosoft Access] KeinzulässigerDateiname.(-1044)(SQLDriverConnect)')

The error I get is: pyodbc.Error: ('HY000', '[HY000] [Microsoft][ODBC-Treiber für Microsoft Access] Kein zulässiger Dateiname. (-1044) (SQLDriverConnect)')

翻译为文件名不可接受". 我找到了一个相关问题,但是答案对我不起作用(使用pyodbc连接到MS Access 2007(.accdb)数据库).我根据以下输出使用32位python:

Which translates to "the filename is not acceptable". I found a related question, but the answer does not work for me (Connecting to MS Access 2007 (.accdb) database using pyodbc). I use 32 bit python according to the output of:

python -c 'import struct; print struct.calcsize("P") * 8'

和MS Access 32位.

and MS Access 32 bit.

  • 以防万一,我用 os.path.isfile(ACCESS_DATABASE_FILE)检查该文件确实存在
  • 可以使用Access打开文件
  • 使用新的连接字符串打开以前的mdb文件会给出相同的错误消息,这不是预期的行为
  • Just in case, I check with os.path.isfile(ACCESS_DATABASE_FILE) that the file actually exists
  • the file can be opened with Access
  • opening the previous mdb file with the new connection string gives the same error message, which afaik is not the expected behavior

推荐答案

好,很抱歉回答我自己的问题,但是通过反复试验,我了解到,如果您使用第二个连接字符串,则需要指定绝对路径名:

Ok, sorry to answer my own question, but by playing around, I learned that you need to specify the absolute path name if you use the second connection string:

ACCESS_DATABASE_FILE = 'C:\\path\\to\\PSA_TEST.accdb'
ODBC_CONN_STR = 'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;' % ACCESS_DATABASE_FILE

然后,它甚至可以与accdb文件以及mdb文件一起使用.

Then it even works with the accdb file, as well as with the mdb file as expected.

这篇关于Pyodbc和MS Access 2010连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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