无法将 pyODBC 与 SQL Server 2008 Express R2 连接 [英] Unable to connect pyODBC with SQL Server 2008 Express R2
本文介绍了无法将 pyODBC 与 SQL Server 2008 Express R2 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码连接 SQL 2008 R2:
I am using following code to connect with 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 地址.附加屏幕截图
I am not sure whether SQL connecting via named Pipes or TCP, I did enable IP Address. Attaching Screen Shot
推荐答案
以下测试代码适用于我将 Python 2.7.5 与 SQL Server 2008 R2 Express Edition 连接:
The following test code works for me to connect Python 2.7.5 with 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:
and the following connection string also works for me because my \SQLEXPRESS instance is listening on port 52865:
connStr = (
r'Driver={SQL Server};' +
r'Server=127.0.0.1,52865;' +
r'Database=myDb;' +
r'Trusted_Connection=Yes;'
)
这篇关于无法将 pyODBC 与 SQL Server 2008 Express R2 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文