0

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.

APC
  • 144,005
  • 19
  • 170
  • 281
Sujit
  • 1
  • 1
  • 1
  • 1
  • An alternative approach would be to [install](https://www.microsoft.com/en-us/download/details.aspx?id=53339) Microsoft ODBC Driver. Then replace `DRIVER{SQLServer};` with `DRIVER={ODBC Driver 13 for SQL Server};` – mondieki Mar 08 '17 at 06:07

3 Answers3

3

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()
some user
  • 337
  • 1
  • 6
  • 16
joshpierro
  • 216
  • 3
  • 6
1

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.

Ashwin Kumar
  • 1,228
  • 7
  • 24
-2

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

pypyodbc
  • 1,119
  • 1
  • 12
  • 9
  • Hello - I installed your package yesterday and am able to use my sql-login to get to the data - thanks for all the work done on this. Can I use Windows authentication to connect to SQL-Server using pypyodbc? – whytheq Mar 24 '16 at 09:11