Skip to content

Instantly share code, notes, and snippets.

@oldarmyc
Created December 10, 2018 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oldarmyc/b30f722b2f0d9e89478f869e038fdb35 to your computer and use it in GitHub Desktop.
Save oldarmyc/b30f722b2f0d9e89478f869e038fdb35 to your computer and use it in GitHub Desktop.
MSSQL example connector
"""
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