Skip to content

Instantly share code, notes, and snippets.

@lukemurraynz
Last active August 16, 2022 23:24
Show Gist options
  • Save lukemurraynz/6636632309bc2bf2b1b37676ee0881ce to your computer and use it in GitHub Desktop.
Save lukemurraynz/6636632309bc2bf2b1b37676ee0881ce to your computer and use it in GitHub Desktop.
python.sql.ipynb
#Libraries
import pyodbc
#Connection to SQL database
server = 'tcp:SQLSERVER.database.windows.net'
database = 'DBNAME'
username = 'user@contoso.com'
password = 'password'
connection = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server='+server+',1433;Database='+database+';Uid='+username+';Pwd='+password+';Encrypt=yes;TrustServerCertificate=no;Connection Timeout=180;Authentication=ActiveDirectoryInteractive')
cursor = connection.cursor()
#Sample select query
cursor.execute("SELECT @@version;")
row = cursor.fetchone()
while row:
print(row[0])
row = cursor.fetchone()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment