How to connect MS Access to Python using pyodbc

荒凉一梦 提交于 2019-12-03 17:15:30

问题


I'm having trouble connecting a database in access with pyodbc. I've seen other example codes that appear near identical to mine that work:

import pyodbc 
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=PYODBC.accdb;UID=me;PWD=pass')
cursor = cnxn.cursor()

cursor.execute("SELECT Forename FROM Student")
row = cursor.fetchone()
if row:
    print(row)

My machine is running on windows 7 home premium 64-bit. I have Microsoft office 2010; 32-bit I'm running python 3.3; 32-bit

I have no idea whats wrong with it, I don't even get an error message, the shell opens, but nothing happens. Any help is greatly appreciated


回答1:


Since you are using the 32-bit versions of both Microsoft Office and Python you should be good to go once you have the right connection string. It should look like this:

connStr = (
    r"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};"
    r"DBQ=C:\full\path\to\your\PYODBC.accdb;"
    )
cnxn = pyodbc.connect(connStr)



回答2:


I am using Win10 and Office 365, my problem resolved with installing Microsoft Access Database Engine 2016 Redistributable

Microsoft Access Database Engine 2016 Redistributable



来源:https://stackoverflow.com/questions/28708772/how-to-connect-ms-access-to-python-using-pyodbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!