mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
import pyodbc
|
|
server = 'your_server.database.windows.net'
|
|
database = 'your_database'
|
|
username = 'your_user'
|
|
|
|
from azure.identity import DefaultAzureCredential
|
|
from azure.keyvault.secrets import SecretClient
|
|
|
|
credential = DefaultAzureCredential()
|
|
|
|
secret_client = SecretClient(vault_url="https://<your_keyvault_name>.vault.azure.net", credential=credential)
|
|
|
|
# NOTE: please replace the ("<your-secret-name>") with the name of the secret in your vault
|
|
secret = secret_client.get_secret("AppSecret")
|
|
|
|
password = secret.value
|
|
|
|
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
|
|
cursor = cnxn.cursor()
|
|
|
|
|
|
print ('Inserting a new row into table')
|
|
#Insert Query
|
|
tsql = "INSERT INTO Employees (Name, Location) VALUES (?,?);"
|
|
with cursor.execute(tsql,'Jake','United States'):
|
|
print ('Successfully Inserted!')
|
|
|
|
|
|
#Update Query
|
|
print ('Updating Location for Nikita')
|
|
tsql = "UPDATE Employees SET Location = ? WHERE Name = ?"
|
|
with cursor.execute(tsql,'Sweden','Nikita'):
|
|
print ('Successfully Updated!')
|
|
|
|
|
|
#Delete Query
|
|
print ('Deleting user Jared')
|
|
tsql = "DELETE FROM Employees WHERE Name = ?"
|
|
with cursor.execute(tsql,'Jared'):
|
|
print ('Successfully Deleted!')
|
|
|
|
|
|
#Select Query
|
|
print ('Reading data from table')
|
|
tsql = "SELECT Name, Location FROM Employees;"
|
|
with cursor.execute(tsql):
|
|
row = cursor.fetchone()
|
|
while row:
|
|
print (str(row[0]) + " " + str(row[1]))
|
|
row = cursor.fetchone() |