Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. import pymysql
  2.  
  3. # the last two arguments are not mandatory
  4. # utfmb4 will provide better character support
  5. # DictCursor will let you access rows with column names rather than index
  6. # or in other words, it will fetch resultset elements as dicts, rather than lists
  7. # e.g. row[0], row[1] <=>  row['id'], row['name']
  8.  
  9. connection = pymysql.connect(host='yourhost', user='user', password='passwd', db='db', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
  10.  
  11. try:
  12.     with connection.cursor() as cursor:
  13.     sql = "SELECT `id`, `first_name`, `last_name` FROM `users` WHERE `email`=%s"
  14.     cursor.execute(sql, ('john.doe@examp.le',))
  15.     result = cursor.fetchone()
  16.     #do_seomthing_with_result
  17. finally:
  18.     connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement