Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import pymysql.cursors
  2.  
  3. # Connect to the database
  4. connection = pymysql.connect(host='localhost',
  5. user='user',
  6. password='passwd',
  7. db='db',
  8. charset='utf8mb4',
  9. cursorclass=pymysql.cursors.DictCursor)
  10.  
  11. try:
  12. with connection.cursor() as cursor:
  13. # Create a new record
  14. sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
  15. cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
  16.  
  17. # connection is not autocommit by default. So you must commit to save
  18. # your changes.
  19. connection.commit()
  20.  
  21. with connection.cursor() as cursor:
  22. # Read a single record
  23. sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
  24. cursor.execute(sql, ('webmaster@python.org',))
  25. result = cursor.fetchone()
  26. print(result)
  27. finally:
  28. connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement