Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql
- # the last two arguments are not mandatory
- # utfmb4 will provide better character support
- # DictCursor will let you access rows with column names rather than index
- # or in other words, it will fetch resultset elements as dicts, rather than lists
- # e.g. row[0], row[1] <=> row['id'], row['name']
- connection = pymysql.connect(host='yourhost', user='user', password='passwd', db='db', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
- try:
- with connection.cursor() as cursor:
- sql = "SELECT `id`, `first_name`, `last_name` FROM `users` WHERE `email`=%s"
- cursor.execute(sql, ('john.doe@examp.le',))
- result = cursor.fetchone()
- #do_seomthing_with_result
- finally:
- connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement