Sujit
Sujit

Reputation: 1

python 3.3, pypyodbc, connecting to SQL Server Fails

I am using python 3.3 + pypyodbc. When I try:

connection = pypyodbc.connect("DRIVER{SQLServer};
                               SERVER=serverIP;
                               UID=myid;
                               PWD=mypwd;
                               DATABASE=mydb")

I get an error:

pypyodbc.DatabaseError: ('08001', '[08001] 
                                   [Microsoft]
                                   [ODBC SQL Server Driver]
                                   [DBNETLIB]Invalid connection.')

I know the IP and credentials are correct, I use them every day to query server using Microsoft SQL Server Management Studio Express. What am I missing here?

Thanks.

Upvotes: 0

Views: 10254

Answers (3)

Ashwin Kumar
Ashwin Kumar

Reputation: 1248

Try something like this

import pypyodbc
conn = pypyodbc.connect(driver='{SQL Server}', server='servername', database='dbname', uid='userName', pwd='Password')

Change the servername and other values with your credentials. It Works perfectly for me. If you are using an azure sql server, make sure you add your IP to the firewall rules.

Upvotes: 1

joshpierro
joshpierro

Reputation: 216

try something like this...

import pypyodbc

connection_string ='Driver={SQL Server Native Client 11.0};Server=YOURSERVER;Database=YOURDATABASE;Uid=YOURUSER;Pwd=YOURPASSWORD;'

connection = pypyodbc.connect(connection_string)

SQL = 'SELECT * FROM <YOURTABLE>'

cur = connection.cursor()
cur.execute(SQL)

cur.close()
connection.close()

Upvotes: 3

pypyodbc
pypyodbc

Reputation: 1139

The connection string is not correct, Google for a correct sqlserver's ODBC connection string

Upvotes: -2

Related Questions