Python 从 MS Access 导入:"Invalid connection string attribute"

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

我正在尝试从 MS Access 数据库导入数据

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)

但是我得到这个错误

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

import pypyodbc 不属于 ODBC 连接字符串。此外,连接字符串可能会被换行符和白色混淆 space。你应该有更好的运气:

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