Last active
August 16, 2022 23:24
-
-
Save lukemurraynz/6636632309bc2bf2b1b37676ee0881ce to your computer and use it in GitHub Desktop.
python.sql.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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