Python Forum
Error in running MS Access Database connection code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in running MS Access Database connection code
#1
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.
Reply
#2
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')
Reply
#3
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.
Reply
#4
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
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Internet connection when running a Python script basil_555 8 579 Mar-11-2024, 11:02 AM
Last Post: snippsat
  problem in running a code akbarza 7 634 Feb-14-2024, 02:57 PM
Last Post: snippsat
  writing and running code in vscode without saving it akbarza 1 380 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  Database connection problems [login, PyQt5, PySql] gradlon93 5 693 Dec-15-2023, 05:08 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 519 Nov-10-2023, 08:09 AM
Last Post: akbarza
  error "cannot identify image file" part way through running hatflyer 0 666 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Pytest Installed, but VS Code Won’t Access Pytest AstralWeeks 9 3,224 Sep-13-2023, 03:00 PM
Last Post: AstralWeeks
  Error when running kivy on python janeik 8 2,033 Jun-16-2023, 10:58 PM
Last Post: janeik
  Running 3rd party libs on Steam Deck (Arch Linux) with restricted access metulburr 0 1,838 Jan-07-2023, 10:41 PM
Last Post: metulburr
  Getting error when running "MINUS" between 2 databases marlonbown 4 1,260 Nov-10-2022, 05:49 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020