Unable to connect pyODBC with SQL Server 2008 Express R2

我怕爱的太早我们不能终老 提交于 2019-12-03 09:11:45

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()

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