Python MySQL Insert Into Table

Python MySQL Insert Into Table

Insert Into Table

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


Insert a record in the “customers” table:

Run example »

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:


Fill the “customers” table with data:

Run example »

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.


Insert one row, and return the ID:

Run example »