Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to sql server 2008 via an IP address using pyodbc in Python

Tags:

python

sql

pyodbc

I am able to connect to server with mssql management studio but not able to connect using python I think some problem in connection string please help below is string I am using.

import pyodbc as p

connStr = ( r'DRIVER={SQL Server};Server=ip; Network=DBMSSOCN;Initial Catalog=' + database + ';User ID=' + id +';Password=' + pass1 +';Trusted_Connection=True' +';')

conn = p.connect(connStr)

error is like below

 conn = p.connect(connStr)
pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][TCP/IP Sock
ets]SQL Server does not exist or access denied. (17) (SQLDriverConnectW); [01000
] [Microsoft][ODBC SQL Server Driver][TCP/IP Sockets]ConnectionOpen (Connect()).
 (10060); [01S00] [Microsoft][ODBC SQL Server Driver]Invalid connection string a
ttribute (0)')
like image 681
sagar Avatar asked Jan 10 '11 12:01

sagar


1 Answers

After lots of trial and error this string ended up working:

connStr = ('DRIVER={SQL Server Native Client 10.0};Server=ip;port=port;Network Library=DBMSSOCN;Database=TEST;uid=id;pwd=pass;')
like image 140
sagar Avatar answered Oct 11 '22 13:10

sagar