Latest web development tutorials

SQLite Insert statement

SQLite TheINSERT INTO statement is used to add new rows to a table in the database.

grammar

INSERT INTO statement has two basic syntax is as follows:

INSERT INTO TABLE_NAME (column1, column2, column3, ... columnN)]  
VALUES (value1, value2, value3, ... valueN);

Here, column1, column2, ... columnN to the data in the table name of the column is inserted.

If you want to add all the columns in the table values, you do not need to be specified in the query in SQLite column name. But make sure the order of the column values ​​in the table in the same order. SQLite The INSERT INTO syntax is as follows:

INSERT INTO TABLE_NAME VALUES (value1, value2, value3, ... valueN);

Examples

Suppose you have created a table in testDB.db COMPANY, as follows:

sqlite> CREATE TABLE COMPANY (
   ID INT PRIMARY KEY NOT NULL,
   NAME TEXT NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (50),
   SALARY REAL
);

Now, the following statement will create six records in the COMPANY table:

INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00);

INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (2, 'Allen', 25, 'Texas', 15000.00);

INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (3, 'Teddy', 23, 'Norway', 20000.00);

INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (4, 'Mark', 25, 'Rich-Mond', 65000.00);

INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (5, 'David', 27, 'Texas', 85000.00);

INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (6, 'Kim', 22, 'South-Hall', 45000.00);

You can also use the second syntax to create a record in the COMPANY table, as follows:

INSERT INTO COMPANY VALUES (7, 'James', 24, 'Houston', 10000.00);

All the above statement creates the following record in the COMPANY table. The next chapter will teach you how to display all the records from a table.

ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0

Use a table to populate another table

You can use the select statement in a table on a field filled with data to another table. Here is the syntax:

INSERT INTO first_table_name [(column1, column2, ... columnN)] 
   SELECT column1, column2, ... columnN 
   FROM second_table_name
   [WHERE condition];

You can temporarily skip the above statement, you can learn in later chapters of the SELECT and WHERE clauses.