1.13. SQLite Select statement

发布时间 :2025-10-25 12:31:13 UTC      

SQLite’s SELECT Statement is used to get data from an SQLite database table and return it as a result table. These result tables are also called result sets.

1.13.1. Grammar

The basic syntax of the SELECT statement for SQLite is as follows:

SELECT column1, column2, columnN FROM table_name;

Here, column1 column2…. Are the fields of the table, and their values are what you want to get. If you want to get all the available fields, you can use the following syntax:

SELECT * FROM table_name;

1.13.2. Example

Suppose the COMPANY table has 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 that uses the SELECT statement to get and display all of these records. Here, the first two commands are used to set the correctly formatted output.

sqlite>.header on
sqlite>.mode column
sqlite> SELECT * FROM COMPANY;

Finally, the following results will be obtained:

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 want to get only the fields specified in the COMPANY table, use the following query:

sqlite> SELECT ID, NAME, SALARY FROM COMPANY;

The above query produces the following results:

ID          NAME        SALARY
----------  ----------  ----------
1           Paul        20000.0
2           Allen       15000.0
3           Teddy       20000.0
4           Mark        65000.0
5           David       85000.0
6           Kim         45000.0
7           James       10000.0

1.13.3. Set the width of the output column

Sometimes, due to the default width of the column to be displayed .mode column In this case, the output is truncated At this point, you can use the .width num, num…. The command sets the width of the display column, as follows:

sqlite>.width 10, 20, 10
sqlite>SELECT * FROM COMPANY;

Above. .width The command sets the width of the first column to 10, the width of the second column to 20, and the width of the third column to 10. So the above SELECT statement 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

1.13.4. Schema information

Because all of them 点命令 Available only in the SQLite prompt, so when you program with SQLite, you use the following sqlite_master Table SELECT statement to list all tables created in the database:

sqlite> SELECT tbl_name FROM sqlite_master WHERE type = 'table';

Assuming that a unique COMPANY table already exists in testDB.db, the following results are produced:

tbl_name
----------
COMPANY

You can list complete information about the COMPANY table, as follows:

sqlite> SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name = 'COMPANY';

Assuming that a unique COMPANY table already exists in testDB.db, the following results are produced:

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
)
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.