简体   繁体   中英

pyodbc and ms access 2010 connection error

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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