Site icon TestingDocs.com

SQLite INSERT INTO Statement

Overview

The SQLite INSERT INTO statement adds new rows(records) to a database table. In this tutorial, we will learn how to add rows using the INSERT statement.

Syntax

The general syntax of the database statement is as follows:

/> INSERT INTO table_name( column1, column2,…, columnN)
      VALUES ( value1, value2,…, valueN);

We do not need to specify the column(s) name in the SQLite INSERT statement if we add values for all the columns of the database table.

/> INSERT INTO table_name VALUES (value1,value2,value3,…,valueN);

Example

Let’s add rows to a table we created using the CREATE TABLE statement. Let’s add wo rows to the emp table.

sqlite> INSERT INTO emp VALUES(1,’John Smith’,’SALESMAN’,2500);
sqlite> INSERT INTO emp VALUES(2,’Emma Johnson’,’MANAGER’,9500);
sqlite>

To view the rows, we can use the SELECT statement:

sqlite> SELECT * FROM emp;
1|John Smith|SALESMAN|2500
2|Emma Johnson|MANAGER|9500
sqlite>

Let’s format the SELECT query output to display the table headers and set the alignment of the cloumns.

sqlite> .header ON
sqlite> .mode column
sqlite> SELECT * FROM emp;
empno name                   jobtitle           salary
—–      ————           ——–            ——
1            John Smith        SALESMAN  2500
2           Emma Johnson MANAGER   9500
sqlite>

The formatting commands in the example are as follows:

The .header ON command will display the headers of the table.
The .mode column will display the columns in left alignment mode.

SQLite Tutorials

SQLite tutorials on this website:

https://www.testingdocs.com/sqlite-database-tutorials/

For more information on SQLite, visit the official website:

https://www.sqlite.org/

Exit mobile version