Reputation: 1013
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]
Upvotes: 1
Views: 6631
Reputation: 1013
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.
Upvotes: 8