Created
August 6, 2020 15:10
-
-
Save bhaskar253/3fc65374c8834e82755958b537d9599c to your computer and use it in GitHub Desktop.
How to insert, commit and fetch data using PyMySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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