mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
Added pyodbc samples
This commit is contained in:
@ -0,0 +1,23 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user