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\")
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;'
)