mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
24 lines
791 B
Python
24 lines
791 B
Python
import pyodbc
|
|
server = 'tcp:myserver.database.windows.net'
|
|
database = 'mydb'
|
|
username = 'myusername'
|
|
password = 'mypassword'
|
|
|
|
#Connection String
|
|
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
|
|
cursor = cnxn.cursor()
|
|
|
|
#Sample select query
|
|
cursor.execute("SELECT @@version;")
|
|
row = cursor.fetchone()
|
|
while row:
|
|
print row[0]
|
|
row = cursor.fetchone()
|
|
|
|
#Sample insert query
|
|
cursor.execute("INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES ('SQL Server Express New 20', 'SQLEXPRESS New 20', 0, 0, CURRENT_TIMESTAMP )")
|
|
row = cursor.fetchone()
|
|
while row:
|
|
print 'Inserted Product key is ' + str(row[0])
|
|
row = cursor.fetchone()
|