dciliske
dciliske

Reputation: 157

Cannot connect to Access DB using pyodbc

I've been beating my head against this for a few days now. I'm trying to use pyodbc to connect to a Microsoft Access DB, and I can't seem to get the connection string right or something. This is what I'm using:

cnxn = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Path\to\file.accdb')

I keep getting the error:

Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnectW)')

Even though when I run dataSources() I see:

{'MS Access Database': 'Microsoft Access Driver (*.mdb, *.accdb)', 'dBASE Files': 'Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)', 'Excel Files': 'Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)'}

Is there something I'm severely missing here?

Upvotes: 5

Views: 9871

Answers (2)

eowoyn
eowoyn

Reputation: 76

This is a 64-bitness problem. I solved it by using 32-bits python and pyodbc.

Upvotes: 6

mwolfe02
mwolfe02

Reputation: 24207

Try adding Provider=MSDASQL. It's deprecated but it seems to work OK:

cnxn = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};
DBQ=C:\Path\to\file.accdb; Provider=MSDASQL;')

Upvotes: 1

Related Questions