2

Trying to connect to sql server with pypyodbc, but I keep getting the "data source name not found and no default driver specified" error. See below attempt. I'm fairly new to all this, but still thought at least testing a connection would be easier.

import pypyodbc

connection = pypyodbc.connect("DRIVER = {SQL Server}; Server = servername;Trusted_Connection = Yes")
connection.close()
1
  • @Andy lowercase didn't work either
    – phinite
    Commented Sep 4, 2015 at 18:49

3 Answers 3

3

Your connection string is missing the Database specifier and you should remove the spaces from the DRIVER specifier.

connection = pypyodbc.connect("DRIVER={SQL Server}; Server = servername;DATABASE=MyDatabase;Trusted_Connection = Yes")
2
  • Check with removing spaces from the DRIVER specifer like this: DRIVER={SQL Server}. Commented Sep 4, 2015 at 19:00
  • Just make sure you're careful with the driver you're using. I had issues with NULLS and datetimes when using the FreeTDS unixODBC driver. Make sure you grab the sql native client drivers to avoid the issues I've had. Commented Sep 4, 2015 at 19:40
2

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.

0

If you want to make python connection with SQL Server then you my use MySQLdb module available for python 2.7 as well.

import MySQLdb as mdb
connectString=Server={SQL Server};Database={Database Name};UID={UserId};PWD={password}
conn = mdb.connect(connectString) 

alternatively you can use pyodbc.

import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER={SQL Server};DATABASE={Database Name};UID={UserId};PWD={password}   

Not the answer you're looking for? Browse other questions tagged or ask your own question.