如何使用pyodbc将MS Access连接到Python [英] How to connect MS Access to Python using pyodbc
问题描述
我无法连接使用pyodbc访问的数据库.我看过其他示例代码似乎与我的代码完全相同:
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)
我的机器在Windows 7家庭高级版64位上运行. 我有Microsoft Office 2010; 32位 我正在运行python 3.3; 32位
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
我不知道它怎么了,我什至没有收到错误消息,shell打开了,但是什么也没发生.任何帮助都将不胜感激
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
推荐答案
由于您同时使用Microsoft Office和Python的32位版本,因此,一旦拥有正确的连接字符串,就应该可以使用了.它应该看起来像这样:
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)
这篇关于如何使用pyodbc将MS Access连接到Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!