2014-01-19 57 views
1

我使用下面的代碼與SQL 2008 R2連接:無法pyODBC與SQL Server連接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

+0

這是否幫助:http://stackoverflow.com/questions/7753830/mssql2008-pyodbc-previous-sql-was-not-a-query – Torxed

+0

不,它不需要。我只是建立連接,而不是執行SP – Volatil3

+0

是的,但我最初的問題實際上是連接,儘管我的錯誤消息。通常情況下,驅動程序不是編程的根本原因。 – Torxed

回答

3

下面的測試代碼對我的作品到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;' 
    )