Skip to content

Instantly share code, notes, and snippets.

@bhaskar253
Created August 6, 2020 15:10
Show Gist options
  • Save bhaskar253/3fc65374c8834e82755958b537d9599c to your computer and use it in GitHub Desktop.
Save bhaskar253/3fc65374c8834e82755958b537d9599c to your computer and use it in GitHub Desktop.
How to insert, commit and fetch data using PyMySQL
import pymysql.cursors
connection = pymysql.connect(host='localhost',
user='dummy',
password='pass',
db='demo',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
sql = "INSERT INTO student ( roll, name ) VALUES (%s, %s)"
cursor.execute(sql, (15, "/home/test/Desktop"))
cursor.execute(sql, (11, "Nano $ King"))
connection.commit()
with connection.cursor() as cursor:
sql = "SELECT * FROM student"
cursor.execute(sql)
result = cursor.fetchall()
print(result)
finally:
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment