Inserting rows

Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

Insert statement is used to insert records in mysql.

Syntax: INSERT INTO <some table> (<some column names>) VALUES("<some values>");

Example 1:


This programs inserts a new city in the city table, notice the use to db.commit(), this method saves your changes to the database .

Example 2:

Notice the use of \  in line 18, \  is used to split python statements to multiple lines.

Inserting multiple rows

To insert multiple rows in the table use executemany()  method of cursor object.

Syntax: cursor_object.executemany(statement, arguments)

statement: string containing the query to execute.

arguments: a sequence containing values to use within insert statement.

Let’s take an example.


Other Tutorials

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Leave a Reply

Be the First to Comment!

Notify of