Python Forum

Full Version: Error in running MS Access Database connection code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I am a new Python programmer and I have question regarding database. I couldn’t find the “database” category, so I am posting here. I have 64-bit MS Windows 8.1 pro, 2013 MS Office, 32-bit Python 3.6.4, and pypyodbc. I installed 32-bit MS Access driver engine 2010. I was trying to connect to MS Access database, either .mdb or .accdb, with the following code:
import pypyodbc conn_str = (Driver={Microsofr Access Drive (*.mdb, *.accdb)};' DBQ=c:\myPythonProgs\database1.mdb;' 'DATABASE=database1;') cnxn = pypyodbc.connect(conn_str) 
I got an error message, and its last 2 statements are:
File "C:\Python\Pyhton36-32\lib\site-packages\pypyodbc.py", line 985, in ctrl_err
raise Error(state,err_text)
pypyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified')
* End of error message *

“database1” in the clause 'DATABASE=database1;’ is a DSN entry. I also ran the code without it, but getting the same error message.
I replaced ODBC with the OLEDB statement, but again I had the same error message.

I would appreciate if somebody can find out the solution.
Thank you.
try
conn_str = ('Driver={Microsofr Access Drive (*.mdb, *.accdb)}; DBQ=c:\myPythonProgs\database1.mdb; DATABASE=database1;')
or
import pypyodbc cnxn = pypyodbc.win_connect_mdb('c:\myPythonProgs\database1.mdb')
Thanks for the reply. The 2nd option (win_create_mdb) works to create a new database file. The 1st option didn’t work. However, I copied the text from somewhere else in a new program and that program worked. I couldn’t understand why! The copied text is :
import pypyodbc pypyodbc.lowercase = False conn = pypyodbc.connect( r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + r"Dbq=c:\myPythonProgs\dbaccdb1.accdb;") cur = conn.cursor()
I modified my original program to have the above statements and it still didn’t work!
Anyway, my new program file is working.
Good for you. It is strange as this code will create the same connection string as my suggestion 1, without the Database part. I see you use different access file in this string.
also pypyodbc.win_create_mdb should create new mdb file, while I was suggesting pypyodbc.win_connect_mdb
The same program with the old database file "database1.mdb" also worked. I just tried with both types of Access files.

Sorry, I knew the routine with "create", so in rush I just read that word; my mistake. Thanks for correcting me.