Python Insert Into MySQL Database Table

How To Insert Data Into MySQL Database Table Using Python

python mysql insert


import mysql.connector

connection = mysql.connector.connect(
host='localhost',
user='root', password='',
port='3306',
database='test_py'
)
c = connection.cursor()

def insertData():
insert_query = "INSERT INTO `users_2`
"`firstname`, `lastname`, `email`, `age`)
"VALUES (%s,%s,%s,%s)"

vals = ("TheFirstName","TheLastName","TheEmail@gmail.com",42)

c.execute(insert_query,vals)

connection.commit()
print(c.lastrowid)

insertData()

OUTPUT:

Python MySQL Insert Into Table





Share this

Related Posts

Previous
Next Post »