Python

Inserting data in Python

Pinterest LinkedIn Tumblr

Inserting data in Python

Inserting data into a table to store it. Use INSERT INTO table_name (column_names) VALUES (data) statement to insert into the table.

Inserting A Single Row
you can read how to insert a single row into the table.


import mysql.connector as mysql

db = mysql.connect(
    host="localhost",
    user="root",
    passwd="root@123",
    database="realprogrammer"
)

cursor = db.cursor()

query = "INSERT INTO user (name, user_name) VALUES (%s, %s)"
## storing values in a variable
values = ("Real Programmer", "RealProgrammer18")

## executing the query with values
cursor.execute(query, values)

db.commit()

print(cursor.rowcount, "Data Saved")

Write A Comment