Connecting to MS SQL Server with Windows Authentication using Python? -
how connect ms sql server using windows authentication, pyodbc library?
i can connect via ms access , sql server management studio, cannot working connection odbc string python.
here's i've tried (also without 'trusted_connection=yes'
):
pyodbc.connect('trusted_connection=yes', driver='{sql server}', server='[system_name]', database='[databasename]') pyodbc.connect('trusted_connection=yes', uid='me', driver='{sql server}', server='localhost', database='[databasename]') pyodbc.connect('trusted_connection=yes', driver='{sql server}', server='localhost', uid='me', pwd='[windows_pass]', database='[database_name]') pyodbc.connect('trusted_connection=yes', driver='{sql server}', server='localhost', database='[server_name]\\[database_name]') pyodbc.connect('trusted_connection=yes', driver='{sql server}', server='localhost', database='[server_name]\[database_name]') pyodbc.connect('trusted_connection=yes', driver='{sql server}', database='[server_name]\[database_name]')
you can specify connection string 1 long string uses semi-colons (;
) argument separator.
working example:
import pyodbc cnxn = pyodbc.connect(r'driver={sql server};server=.\sqlexpress;database=mydb;trusted_connection=yes;') cursor = cnxn.cursor() cursor.execute("select lastname mycontacts") while 1: row = cursor.fetchone() if not row: break print(row.lastname) cnxn.close()
for connection strings lots of parameters, following accomplish same thing in more readable way:
conn_str = ( r'driver={sql server};' r'server=.\sqlexpress;' r'database=mydb;' r'trusted_connection=yes;' ) cnxn = pyodbc.connect(conn_str)
(note there no commas between individual string components.)
Comments
Post a Comment