Question

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

Which gives error:

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>

I am not sure whether SQL connecting via named Pipes or TCP, I did enable IP Address. Attaching Screen Shot enter image description here

Était-ce utile?

La solution

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;'
    )
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top