Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 406
dciliske Avatar asked Dec 28 '10 19:12

dciliske


2 Answers

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

like image 179
eowoyn Avatar answered Sep 30 '22 04:09

eowoyn


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;')
like image 23
mwolfe02 Avatar answered Sep 30 '22 05:09

mwolfe02