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()
11
12#Sample select query
13cursor.execute("SELECT @@version;")
14row = cursor.fetchone()
15while row:
16 print(row[0])
17 row = cursor.fetchone()