English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
SQLite SELECT
The statement is used to retrieve data from an SQLite database table, which returns data in the form of a result table. These result tables are also known asresult sets
.
The following is the basic syntax of the SQLite SELECT statement.
SELECT column1, column2, columnN FROM table_name;
Here, column1, column2 ...is the field of the table, you want to get its value. If you want to get all available fields in this field, you can use the following syntax-
SELECT * FROM table_name;
Consider the COMPANY table with the following records-
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
The following is an example of using the SELECT statement to retrieve and display all these records. Here, the first three commands are used to set the correct output format.
sqlite>.header on sqlite>.mode column sqlite> SELECT * FROM COMPANY;
Finally, you will get the following result.
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
If you only want to retrieve selected fields from the COMPANY table, use the following query-
sqlite> SELECT ID, NAME, SALARY FROM COMPANY;
The above query will produce the following result.
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
Sometimes,.mode column
Due to the default width of the columns to be displayed, you may encounter issues related to truncated output. What you can do is, you can use the following.width num, num....
The .width command sets the column width that can be displayed:
sqlite>.width 10, 20, 10 sqlite> SELECT * FROM COMPANY;
The above.width
The command sets the width of the first column to10, the width of the second column is set to20, the width of the third column is set to10. Finally, the above SELECT statement will produce the following result.
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
Since all dot commands can be used under the SQLite prompt, when programming with SQLite, you will use the following SELECT statement and list all tables created in the database with the SQLite master table.
sqlite> SELECT tbl_name FROM sqlite_master WHERE type = 'table';
Assuming that your testDB.db only has the COMPANY table, the following result will be produced.
tbl_name----------COMPANY
You can list the complete information about the COMPANY table as follows:
sqlite> SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name = 'COMPANY';
Assuming that your testDB.db only has the COMPANY table, the following result will be produced.
CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL)