Site icon i2tutorials

SQLiteDB – Insert Query

SQLiteDB – Insert Query:

SQLite INSERT can be used to insert the data into the tables in different ways like inserting a single row or multiple rows or a default values in the table.

Now, let us see the syntax for inserting a single row into the table.

Syntax: 

INSERT INTO table1 (

                        column1,

                        column2 ,..)



            VALUES(

                        value1,

                        value2 ,...);

Note:

In the above statement, if you don’t mention columns list then the values list should tally in sequence of columns in the table.

Example:

INSERT INTO CUSTOMERS (cust_id, first_name, last_name, Gender, Email, City)

VALUES (1,"hari","prasad","M","hari@abc.com","hyderabad");

or

INSERT INTO CUSTOMERS

VALUES (1,"hari","prasad","M","hari@abc.com","hyderabad");

Let’s see the syntax for how to insert multiple rows in a table. Please note that multiple insert in SQLite works from version 3.7.11.

Syntax:

INSERT INTO table1 (column1,column2 ,..)

VALUES(value1,value2 ,...),

            (value1,value2 ,...),

                          ...



                        (value1,value2 ,...);

Example:

INSERT INTO CUSTOMERS

            (cust_id, first_name, last_name, Gender, Email, City)

  VALUES

            (2, "mahi", "kumar", "M", "mahi@abc.com", "hyderabad"),

            (3, "sasi", "kala", "F", "sasi@abc.com", "hyderabad");

When you are creating the table, you can assign default values for columns and you can insert them as below.

Example:

INSERT INTO CUSTOMERS DEFAULT VALUES;

You can insert data using the SELECT statement where it will query the data from the table first and the results returned from the query will be inserted into the table.

Example:

INSERT INTO CUSTOMERS_OLD SELECT CUST_id, FIRST_NAME FROM CUSTOMERS;

 

Exit mobile version