Link to home
Start Free TrialLog in
Avatar of PratikShah111
PratikShah111

asked on

Connect to sql server using Python

I am just writing a simple python script to connect to sql server .  SQL server that  I am trying to connect does not have trusted connection and so i have to login using integrated authentication. so in my script  i  try to login using the username and password that i use to login into management studio. but when i execute the script it gives me an error message saying login failed for user "it shows my windows username" and just ignores the username that I am passing in my script.
Below is my script.


server = "xxxxxxx.svr.us.xxxxxx.net\\Servername"
database = "MyDatabase"
username = "username"
password = "password"

con = pyodbc.connect('DRIVER={SQL Server};Server='+server+';Database='+database+';username='+username+';password='+password)
print (con)
con = conStr
cur = con.cursor()   #Generates error on this line
cur.execute("SELECT top 10 * from mytable;")

error that i get

pyodbc.Error: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'OVER HERE I SEE MY WINDOWS USERNAME. (18456) (SQLDriverConnect)")
SOLUTION
Avatar of Systech Admin
Systech Admin
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PratikShah111
PratikShah111

ASKER

i have pyodbc installed
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Initiating auto close as we haven't heard back from author and solution provided..