简体   繁体   中英

pyodbc - ODBC Connection not working

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM