🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #175

MySQL Insert


Insert Into Table

To fill a table in MySQL, use the "INSERT INTO" statement.

Example

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",

user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name,
address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql,
val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")

Note: Important!: Notice the statement: mydb.commit(). It is required to make the changes, otherwise no changes are made to the table.


Insert Multiple Rows

To insert multiple rows into a table, use the executemany() method.

The second parameter of the executemany() method is a list of tuples, containing the data you want to insert:

Example

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",

user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name,
address) VALUES (%s, %s)"
val = [
  ('Peter', 'Lowstreet 4'),

('Amy', 'Apple st 652'),
  ('Hannah', 'Mountain 21'),

('Michael', 'Valley 345'),
  ('Sandy', 'Ocean blvd 2'),

('Betty', 'Green Grass 1'),
  ('Richard', 'Sky st 331'),

('Susan', 'One way 98'),
  ('Vicky', 'Yellow Garden 2'),

('Ben', 'Park Lane 38'),
  ('William', 'Central st 954'),

('Chuck', 'Main Road 989'),
  ('Viola', 'Sideway 1633')
]

mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

Get Inserted ID

You can get the id of the row you just inserted by asking the cursor object.

Note: If you insert more than one row, the id of the last inserted row is returned.

Example

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",

user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name,
address) VALUES (%s, %s)"
val = ("Michelle", "Blue Village")
mycursor.execute(sql, val)

mydb.commit()

print("1 record
inserted, ID:", mycursor.lastrowid)

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

MySQL Insert – FAQs

Quick answers about learning MySQL Insert in Python.

This free note from CodingNow 2.0 explains MySQL Insert in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including MySQL Insert, is 100% free with no signup required.
With focused practice, most students grasp MySQL Insert in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now