1

I can't connect to a Microsoft Access database using pyodbc as below:

import pyodbc

conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\zghid\Desktop\Database1.accdb;')
cursor = conn.cursor()
cursor.execute('select * from names_table')

cursor.execute('''
               INSERT INTO table1(nom, prenom, age)
               VALUES('Mike', 'Freeman', '23')
               ''')

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

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)')

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
someone
  • 53
  • 1
  • 8
  • 1
    What's output do you get when you run `[x for x in pyodbc.drivers() if x.startswith('Microsoft Access Driver')]`? – Mihai Chelaru Jul 28 '19 at 16:03
  • instead of the for loop ? – someone Jul 28 '19 at 16:07
  • No, this is from the [`pyodbc` GitHub wiki on connecting to Microsoft Access](https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-Microsoft-Access). It's to check which ODBC drivers you have installed. Have you read through how to set up the drivers correctly? It seems there's some important steps in there to make sure you have the right drivers. Do you have 64-bit or 32-bit Office? – Mihai Chelaru Jul 28 '19 at 16:11
  • i have 32bit office – someone Jul 28 '19 at 16:29
  • 1
    Run that code that I gave you above in an interactive Python shell. Open up Python and do `import pyodbc`, then run that list comprehension and see if the list is empty. I suspect that, as stated in the docs, you have 64-bit Python and 32-bit Office, so you need to install the 64-bit version of the ACE driver. According to the docs, it's better to just have 32-bit Python installed and use that with your 32-bit Office instead of having both 32-bit and 64-bit drivers, as it might break your Office installation. All this information is on that page I linked. – Mihai Chelaru Jul 28 '19 at 16:37
  • 1
    okay ill try this, i hope it works – someone Jul 28 '19 at 16:49

0 Answers0