Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PYODBC InterfaceError- Data source name not found

I am trying to connect Python to MS Access Database using pyodbc but every time I get the following error:

pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

And this what I have written to connect python to MS Access:

import pyodbc

conn = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\PILOT_DATA.accdb;')
cursor = conn.cursor()
cursor.execute('select * from p_inventor')

for row in cursor.fetchall():
    print (row)

According to the error, it doesn't find the Data source name and so I changed the 'DRIVER' to 'DSN'

import pyodbc

conn = pyodbc.connect(r'DSN={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\PILOT_DATA.accdb;')
cursor = conn.cursor()
cursor.execute('select * from p_inventor')

for row in cursor.fetchall():
    print (row)

But it doesn't help. I get the following error:

pyodbc.Error: ('IM010', '[IM010] [Microsoft][ODBC Driver Manager] Data source name too long (0) (SQLDriverConnect)')

Other workaround I have tried is to use both python 32 and 64 bit

Here goes the version details:

  • Python 3.7.4 64 bit
  • pip 19.2.3
  • pyodbc-4.0.27
  • Office365 16

Would be really helpful to know what else I can do to connect Python to ACCESS database. Thanks in Advance!

like image 280
Tawhidul Islam Avatar asked Oct 28 '25 00:10

Tawhidul Islam


2 Answers

I have solved this issue by installing the Access Database Engine. In order to do that, I had to unistall the office365 program-> install access database engine-> re-install office365. And then the code runs perfectly!

like image 94
Tawhidul Islam Avatar answered Oct 30 '25 16:10

Tawhidul Islam


Installing the Access Driver on Microsoft website worked for me.

Before installing the driver I also got an InterfaceError and running

 import pyodbc
 [x for x in pyodbc.drivers() if x.startswith("Microsoft Access Driver")]

yielded an empty list [].

After installing the driver (x64 version), it appears in the list from the code above and calling conn.cursor() doesn't yield the error anymore:

import pyodbc
[x for x in pyodbc.drivers() if x.startswith("Microsoft Access Driver")]
Out[1]: ['Microsoft Access Driver (*.mdb, *.accdb)']
like image 23
Luc M Avatar answered Oct 30 '25 16:10

Luc M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!