Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyodbc - ODBC Connection not working

Tags:

python

pyodbc

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

like image 960
user2320821 Avatar asked Dec 02 '25 02:12

user2320821


1 Answers

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

like image 96
user2320821 Avatar answered Dec 03 '25 18:12

user2320821