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

2 Comments on "Inserting rows"

Notify of
avatar

Sort by:   newest | oldest | most voted
debasish kar
Guest
debasish kar
5 months 4 days ago

i got the real help from this Blog ….so warm thanks to you

Labyras
Guest
Labyras
5 months 21 days ago

I could not find the db.commit command anywhere on the internet, it was the missing piece in my code. Thank you so much!

wpDiscuz