Insert Into Python MySql
Mysql database में row insert या ऐड करना है तो INSERT INTO SQL स्टेटमेंट का उपयोग करना होता है।
import mysql.connector
db = mysql.connector.connect(
host="localhost", user="username", passwd="password", database="dbname"
)
crsr = = mydb.cursor()
sqlcmd = "INSERT INTO employee(name, address) VALUES (%s,%s)"
vlue = ("Ram", "India")
crsr.execute(sqlcmd, vlue)
db.commit()
print(crsr.rowcount, "record successfully inserted.")
db.close()
commit() फंक्शन का उपयोग करना ज़रूरी है नहीं तो operation परफॉर्म नहीं होगा database में।
Insert multiple rows
import mysql.connector
db = mysql.connector.connect(
host="localhost", user="username", passwd="password", database="dbname"
)
crsr = = mydb.cursor()
sqlcmd = "INSERT INTO employee(name, address) VALUES (%s,%s)"
vlue = [
("Ram", "India"),
("Shyam", "Bharat"),
("Gopal", "Hindustaan")
]
crsr.execute(sqlcmd, vlue)
db.commit()
print(crsr.rowcount, "record successfully inserted.")
db.close()
Get ID of last row inserted
import mysql.connector db = mysql.connector.connect( host="localhost", user="username", passwd="password", database="dbname" ) crsr = = mydb.cursor() sqlcmd = "INSERT INTO employee(name, address) VALUES (%s,%s)" vlue = ("Ram", "India") crsr.execute(sqlcmd, vlue) db.commit() print(crsr.rowcount, "record successfully inserted.", crsr.lastrowid) db.close()
Python class part 👉 34
Go to next class part 👉 35
0 टिप्पणियाँ
If you have any doubt. Please let me know