Python Tutorials · Python MySQL

MySQL Insert

Learn all about MySQL Insert in this comprehensive tutorial.

5 min read advanced
  • To fill a table in MySQL, use the "INSERT INTO" statement.
  • To insert multiple rows into a table, use the executemany() method.
  • You can get the id of the row you just inserted by asking the cursor object.

Insert Into Table

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

python
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:

python

Get Inserted ID

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

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

Module quiz

2 questions
1

Which of the following is true about MySQL Insert?

2

What is the most common pitfall when working with MySQL Insert?

Answer all questions to submit.