Guest User

Untitled

a guest
Dec 13th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import pyodbc
  2. server = 'localhost'
  3. database = 'SampleDB'
  4. username = 'sa'
  5. password = 'your_password'
  6. cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';PORT=1443;DATABASE='+database+';UID='+username+';PWD='+ password)
  7. cursor = cnxn.cursor()
  8.  
  9. print ('Inserting a new row into table')
  10. #Insert Query
  11. tsql = "INSERT INTO Employees (Name, Location) VALUES (?,?);"
  12. with cursor.execute(tsql,'Jake','United States'):
  13. print ('Successfuly Inserted!')
  14.  
  15.  
  16. #Update Query
  17. print ('Updating Location for Nikita')
  18. tsql = "UPDATE Employees SET Location = ? WHERE Name = ?"
  19. with cursor.execute(tsql,'Sweden','Nikita'):
  20. print ('Successfuly Updated!')
  21.  
  22.  
  23. #Delete Query
  24. print ('Deleting user Jared')
  25. tsql = "DELETE FROM Employees WHERE Name = ?"
  26. with cursor.execute(tsql,'Jared'):
  27. print ('Successfuly Deleted!')
  28.  
  29.  
  30. #Select Query
  31. print ('Reading data from table')
  32. tsql = "SELECT Name, Location FROM Employees;"
  33. with cursor.execute(tsql):
  34. row = cursor.fetchone()
  35. while row:
  36. print (str(row[0]) + " " + str(row[1]))
  37. row = cursor.fetchone()
Add Comment
Please, Sign In to add comment