pyodbc and ms access 2010 connection error

喜欢而已 提交于 2019-12-08 06:18:43

问题


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 

Now I use:

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)

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

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'

and MS Access 32 bit.

[EDIT]

  • 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

回答1:


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

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



来源:https://stackoverflow.com/questions/18806349/pyodbc-and-ms-access-2010-connection-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!