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

时间:2014-01-19 09:35:47

标签: python sql-server sql-server-2008 windows-xp pyodbc

我使用以下代码连接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

1 个答案:

答案 0 :(得分:4)

以下测试代码可用于将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;'
    )