phinite
phinite

Reputation: 35

pypyodbc connection to sql server

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()

Upvotes: 2

Views: 7305

Answers (3)

Ashwin Kumar
Ashwin Kumar

Reputation: 1258

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: 2

Chetan chadha
Chetan chadha

Reputation: 568

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}   

Upvotes: 0

Thane Plummer
Thane Plummer

Reputation: 10348

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")

Upvotes: 3

Related Questions