pymysql insert dynamic

Estimated reading: 1 minute 315 views

Python insert Dynamic Operation

we need to use placeholder.

With MYSqLdb, you should use a placeholder of %s to format all the data value as string.

MYSQL will perform type conversion as necessery.

if we wanted to give multiple value as placeholder we can use tuple.

				
					import pymysql
conn = pymysql.connect(
        host='localhost',
        user='root',
        password="12345",
        db='sakila',
)

get_id = int(input("enter the id"))
get_name = input("enter the name")

cur = conn.cursor()
str1 = "INSERT INTO teacher VALUES (%s,%s)"
cur.execute(str1, (get_id, get_name))
conn.commit()
conn.close()
				
			

Leave a Comment

CONTENTS