Smith Smith - 5 months ago 34
Python Question

Python importing from MS Access: "Invalid connection string attribute"

I am trying to import data from a MS Access database

import pyodbc

connStr = """

import pypyodbc
Driver={Microsoft Access Driver (*.mdb, *.accdb)}
DBQ = C:\\Users\\haesh\\Desktop\\Comp Sci CC\\Database1.accdb;

"""
cnxn = pyodbc.connect(connStr)


However I get this error

Traceback (most recent call last):
File "C:\Users\haesh\Desktop\Comp Sci CC\qwerty.py", line 10, in <module>
cnxn = pyodbc.connect(connStr)
pyodbc.Error: ('01S00', '[01S00] [Microsoft][ODBC Driver Manager] Invalid connection string attribute (0) (SQLDriverConnect)')

Answer

The import pypyodbc does not belong in the ODBC connection string. Also, connection strings can be confused by line breaks and white space. You should have better luck with this:

connStr = (
    r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
    r"DBQ=C:\Users\haesh\Desktop\Comp Sci CC\Database1.accdb;"
    )
Comments