Advertisement
Guest User

sqlpython,py

a guest
Sep 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. import mysql.connector
  2.  
  3. user = 'root';
  4. password = 'root';
  5.  
  6. cnx = mysql.connector.connect(user=user, password=password,
  7.                               host='127.0.0.1',
  8.                               database='cs360')
  9.                              
  10. cursor = cnx.cursor();
  11.  
  12. dropTable = "DROP TABLE IF EXISTS Employee";
  13. cursor.execute(dropTable);                           
  14. employee = "CREATE TABLE Employee (Id INT NOT NULL AUTO_INCREMENT,Name VARCHAR(200),Salary INT,DepartmentId INT,PRIMARY KEY (Id))";                      
  15. cursor.execute(employee);                            
  16.                              
  17. addEmployee = "INSERT INTO Employee (Name, Salary, DepartmentId) VALUES (%s, %s, %s)";                           
  18. dataEmployee = [('Joe', 70000, 1), ('Henry', 80000, 2)];             
  19. cursor.executemany(addEmployee, dataEmployee);                   
  20.  
  21. query = ("select * from Employee");
  22. cursor.execute(query);
  23. for (Id, Name, Salary, DepartmentId) in cursor:
  24.   print Id, Name, Salary, DepartmentId                           
  25.                              
  26. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement