1.13. SQLite Select statement

发布时间 : 2025-10-25 13:32:44 UTC      

Page Views: 10 views

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
)
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。