gpt4 book ai didi

python - 无法将 pyODBC 与 SQL Server 2008 Express R2 连接

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:59 27 4
gpt4 key购买 nike

我正在使用以下代码连接 SQL 2008 R2:

cnxnStoneedge = pyodbc.connect("DRIVER={SQL Server};SERVER=127.0.0.1;DATABASE=myDB;UID=admin;PWD=admin")

给出错误:

Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect)')
args = ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNE...t exist or access denied. (17) (SQLDriverConnect)')
with_traceback = <built-in method with_traceback of Error object>

我不确定 SQL 是通过命名管道还是 TCP 连接,我确实启用了 IP 地址。附加屏幕截图 enter image description here

最佳答案

以下测试代码适用于将 Python 2.7.5 与 SQL Server 2008 R2 Express Edition 连接:

# -*- coding: utf-8 -*-
import pyodbc

connStr = (
r'Driver={SQL Server};' +
r'Server=(local)\SQLEXPRESS;' +
r'Database=myDb;' +
r'Trusted_Connection=Yes;'
)

db = pyodbc.connect(connStr)

cursor1 = db.execute('SELECT [word] FROM [vocabulary] WHERE [ID]=5')

while 1:
row = cursor1.fetchone()
if not row:
break
print row.word
cursor1.close()
db.close()

以下连接字符串也适用于我,因为我的\SQLEXPRESS 实例正在监听端口 52865:

connStr = (
r'Driver={SQL Server};' +
r'Server=127.0.0.1,52865;' +
r'Database=myDb;' +
r'Trusted_Connection=Yes;'
)

关于python - 无法将 pyODBC 与 SQL Server 2008 Express R2 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21215128/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com