Inserting rows

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.

 

Leave a Reply

Be the First to Comment!

Notify of
avatar

wpDiscuz