Created
December 10, 2018 18:10
-
-
Save oldarmyc/b30f722b2f0d9e89478f869e038fdb35 to your computer and use it in GitHub Desktop.
MSSQL example connector
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
""" | |
In order to use the example you must install pymssql from conda | |
conda install -c anaconda pymssql | |
""" | |
import pymssql | |
# Using an ini style credentials file for example so will use configparser | |
import configparser | |
# Setup config parser and read kubernetes secret file | |
config = configparser.ConfigParser() | |
config.read('/var/run/secrets/user_credentials/mssql_credentials') | |
# Setup URI and database to use | |
server = 'example-mssql.dev.anaconda.com' | |
database = 'SampleDB' | |
# Define the connection using variables pulled from secret | |
connection = pymssql.connect( | |
server, | |
config.get('default', 'username'), | |
config.get('default', 'password'), | |
database | |
) | |
# Setup the cursor and execute an example query | |
cursor = connection.cursor() | |
cursor.execute(""" | |
SELECT TOP (10) [AddressID] | |
,[AddressLine1] | |
,[AddressLine2] | |
,[City] | |
,[StateProvinceID] | |
,[PostalCode] | |
,[SpatialLocation] | |
,[rowguid] | |
,[ModifiedDate] | |
FROM [AdventureWorks2016].[Person].[Address] | |
""") | |
# Print the results from the query | |
row = cursor.fetchone() | |
while row: | |
print(row) | |
row = cursor.fetchone() | |
# Close the connection once complete | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment