Advertisement
Guest User

Untitled

a guest
Jul 9th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import pymysql.cursors
  2. #Here we connect to the database
  3. connection = pymysql.connect(host='mysql.cc.puv.fi',
  4. user='e1301181',
  5. password='4cuJnkhwmRPE',
  6. db='e1301181_Python',
  7. charset='utf8mb4',
  8. cursorclass=pymysql.cursors.DictCursor)
  9. #Insert
  10. try:
  11. with connection.cursor() as cursor:
  12. # Here we create a new record
  13. name = input("Give a name of the customer:\n")
  14. address = input("Give an address for the customer '"+name+"'\n")
  15. phone = input("Give a phone number for the customer '"+name+"'\n")
  16. sql = "INSERT INTO CUSTOMER (address, name, phone) VALUES ('{address}', '{name}', '{phone}')".format(address=address, name=name, phone=phone)
  17. cursor.execute(sql)
  18. # Changes are not autocommit by default. So we must commit to save
  19. # changes.
  20. connection.commit()
  21.  
  22. with connection.cursor() as cursor:
  23. #Here we make a query
  24. sql = "SELECT address, name, phone FROM CUSTOMER WHERE name=%s"
  25. cursor.execute(sql, (name))
  26. #result = cursor.fetchone()
  27. result = cursor.fetchall()
  28. print(result)
  29. except Exception as e:
  30. print("Error: " + str(e))
  31.  
  32. #update
  33.  
  34. #delete
  35.  
  36. #dispall
  37.  
  38.  
  39. #dispemp
  40.  
  41.  
  42.  
  43. finally:
  44. connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement