Guest User

Untitled

a guest
Dec 10th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. """
  2. In order to use the example you must install pymssql from conda
  3.  
  4. conda install -c anaconda pymssql
  5. """
  6. import pymssql
  7.  
  8.  
  9. # Using an ini style credentials file for example so will use configparser
  10. import configparser
  11.  
  12.  
  13. # Setup config parser and read kubernetes secret file
  14. config = configparser.ConfigParser()
  15. config.read('/var/run/secrets/user_credentials/mssql_credentials')
  16.  
  17.  
  18. # Setup URI and database to use
  19. server = 'example-mssql.dev.anaconda.com'
  20. database = 'SampleDB'
  21.  
  22. # Define the connection using variables pulled from secret
  23. connection = pymssql.connect(
  24. server,
  25. config.get('default', 'username'),
  26. config.get('default', 'password'),
  27. database
  28. )
  29.  
  30. # Setup the cursor and execute an example query
  31. cursor = connection.cursor()
  32. cursor.execute("""
  33. SELECT TOP (10) [AddressID]
  34. ,[AddressLine1]
  35. ,[AddressLine2]
  36. ,[City]
  37. ,[StateProvinceID]
  38. ,[PostalCode]
  39. ,[SpatialLocation]
  40. ,[rowguid]
  41. ,[ModifiedDate]
  42. FROM [AdventureWorks2016].[Person].[Address]
  43. """)
  44.  
  45. # Print the results from the query
  46. row = cursor.fetchone()
  47. while row:
  48. print(row)
  49. row = cursor.fetchone()
  50.  
  51. # Close the connection once complete
  52. connection.close()
Add Comment
Please, Sign In to add comment