SQLite Select 语句


sqlite 的 select 语句用于从 sqlite 数据库表中获取数据,以结果表的形式返回数据。这些结果表也被称为结果集。

 

1. 语法

sqlite 的 select 语句的基本语法如下:

select column1, column2, columnn from table_name;

在这里,column1, column2...是表的字段,他们的值即是您要获取的。如果您想获取所有可用的字段,那么可以使用下面的语法:

select * from table_name;

 

2. 范例

假设 company 表有以下记录:

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

下面是一个范例,使用 select 语句获取并显示所有这些记录。在这里,前两个个命令被用来设置正确格式化的输出。

sqlite>.header on
sqlite>.mode column
sqlite> select * from company;

最后,将得到以下的结果:

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

如果只想获取 company 表中指定的字段,则使用下面的查询:

sqlite> select id, name, salary from company;

上面的查询会产生以下结果:

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

 

3. 设置输出列的宽度

有时,由于要显示的列的默认宽度导致 .mode column,这种情况下,输出被截断。此时,您可以使用 .width num, num.... 命令设置显示列的宽度,如下所示:

sqlite>.width 10, 20, 10
sqlite>select * from company;

上面的 .width 命令设置第一列的宽度为 10,第二列的宽度为 20,第三列的宽度为 10。因此上述 select 语句将得到以下结果:

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

 

4. schema 信息

因为所有的点命令只在 sqlite 提示符中可用,所以当您进行带有 sqlite 的编程时,您要使用下面的带有 sqlite_master 表的 select 语句来列出所有在数据库中创建的表:

sqlite> select tbl_name from sqlite_master where type = 'table';

假设在 testdb.db 中已经存在唯一的 company 表,则将产生以下结果:

tbl_name
----------
company

您可以列出关于 company 表的完整信息,如下所示:

sqlite> select sql from sqlite_master where type = 'table' and tbl_name = 'company';

假设在 testdb.db 中已经存在唯一的 company 表,则将产生以下结果:

create table company(
   id int primary key     not null,
   name           text    not null,
   age            int     not null,
   address        char(50),
   salary         real
)

下一节:sqlite 运算符

sqlite教程

相关文章