Python 将 Microsoft Access 2007 数据库连接到 python

问题描述 投票:0回答:1
import pyodbc

db_path = r'E:\MasterMindAcc\Sys\NetNo.accdb'
password = 'AaBbCc'

#Your connection string here...
conn_str = f'DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};DBQ={db_path};PWD={password}'

try:
    #Make the connection...
    conn = pyodbc.connect(conn_str)

    #Add your code for all your database operations here...

    #Close the connection when done...
    conn.close()

except Exception as e:
    print(f"Error: {e}")



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

OS.: Windows 11 IoT Enterprise (64-bit)
Python 3.12.0
Microsoft Access 2007 (32-bit)

print(pyodbc.drivers())
['SQL Server']

想要在access 2007数据库表中查找用户名为Admin的记录并检查用户输入的密码是否正确 但我无法将 2007 数据库连接到 python

python database-connection ms-access-2007
1个回答
0
投票

安装即可

AccessDatabaseEngine_X64.exe /被动 (其中包含 x86 和 x64 版本的驱动程序),您会没事的。不要忘记 /passive 选项,因为如果您这样做,除非您还安装了 MS Office 2010,否则它将无法安装。您可以从 Microsoft Access Database Engine 2010 Redistributable 站点下载该文件

安装 AccessDatabaseEngine_X64.exe 后,您应该在 python shell 上运行以下代码来测试一切正常:

导入pyodbc [x for x in pyodbc.drivers() if x.startswith('Microsoft')] 你应该得到类似的打印输出

['Microsoft Access 驱动程序 (*.mdb, .accdb)', 'Microsoft Excel 驱动程序 (.xls、*.xlsx、*.xlsm、.xlsb)', 'Microsoft Access dBASE 驱动程序 (.dbf、*.ndx、.mdx)', 'Microsoft Access 文本驱动程序 (.txt、*.csv)']

© www.soinside.com 2019 - 2024. All rights reserved.