1import pyodbc
2# Some other example server values are
3# server = 'localhost\sqlexpress' # for a named instance
4# server = 'myserver,port' # to specify an alternate port
5server = 'tcp:myserver.database.windows.net'
6database = 'mydb'
7username = 'myusername'
8password = 'mypassword'
9cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
10cursor = cnxn.cursor()
1conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=test;DATABASE=test;UID=user;PWD=password')
1with pyodbc.connect(conx_string) as conx:
2 cursor = conx.cursor()
3 cursor.execute(query)
4 data = cursor.fetchall()
5