3
votes

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

1
Didn't use it for a long time, but I'm sure that the end of your connection string is wrong. Try to replace "...DBD=DBFile" with "...DBQ=" + DBFileSerge Ballesta
Tried the above and different error received: File "C:/Users/davisr/PycharmProjects/File_Names/ex1.py", line 6 conn = pyodbc.connect("Driver={Microsoft Access Driver(*.mdb, *.accdb)};DBQ="DBfile;)user2320821
I proposed you ...;DBQ=" + DBFile) not ...;DBQ="DBfile;)Serge Ballesta
@user2320821 The problems you are encountering boil down to string formatting. Take a look at the examples and update your question with any specific questions you have.Bryan
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_fileuser2320821

1 Answers

3
votes

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