Python – pyodbc – ODBC Connection not working

pyodbcpython

I am new to Python and trying to use the library pyodbc to connect to an MS Access Database. I have a 32 bit database, and 32 bit drivers. I keep reading but am unable to understand what looks like a simple set of commands.

import pyodbc
DBfile = 'C:/Users/davisr/My Documents/TEMP/Comp_Model_Db_Testing.mdb'

conn = pyodbc.connect("Driver={Microsoft Access Driver(*.mdb, *.accdb)};DBQ=DBfile")

The error that I received is as follows:
C:\Python27\python.exe C:/Users/davisr/PycharmProjects/File_Names/ex1.py
Traceback (most recent call last):
File "C:/Users/davisr/PycharmProjects/File_Names/ex1.py", line 6, in
conn = pyodbc.connect("Driver={Microsoft Access Driver(*.mdb, *.accdb)};DBQ=+DBfile")
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found
List item and no default driver specified (0) (SQLDriverConnect)')

Your help is appreciated. I am sure it is something simple.

Respectfully,

Robert Davis

Best Answer

I took the advice of beargle and Serge Ballesta and the following finally worked:

access_database_file = 'C:\\Users\\davisr\\My Documents\\TEMP\\Comp_Model_Db_Testing.mdb'       
ODBC_CONN_STR = 'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;' %access_database_file–  user23208211 min ago   

Thank you Serge and beargle

Related Topic