SQLite 语句结合函数操作 | 学习笔记

简介: 快速学习 SQLite 语句结合函数操作

开发者学堂课程【嵌入式之 RFID 开发与应用2020版:SQLite 语句结合函数操作】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/665/detail/11229


SQLite 语句结合函数操作

SQL 语句进阶

1.函数和聚合

(1)函数

SQL 语句支持利用函数来处理数据,函数一般是在数据上执行的,它给数据的转换和处理提供了方便

常用的文本处理函数:

length() 返回字符串的长度

lower() 将字符串转换为小写

upper() 将字符串转换为大写

语法: select 函数名(列名) from 表名;

在终端输入:

sqlite?select *from persons;

1| lucy| Beijing

1 |peter| tianjing

3 | bob |Hebei

sqlite> select id, upper (name) from persons;

1|LUCY

1|PETER

3|BOB

可以利用函数进行转换,但转换不是长久的,主要是方便查看。

演示:sqlitez select id ,upper(name ) ,length ( addr) from tbl;

Id          upper(name)          length(addr)

----------   -------------------      --------------

101             ZS                     2

102             LS                     2

103             WW                    2

104              ZS                    2

105              XW                    2

106              XH                    2

最后结果是名称装换为大写,第三列数据显示地址的字符个数。

以上装换都是临时的。

(2)常用的聚集函数:

使用聚集函数,用于检索数据,以便分析和报表生成

avg()     返回某列的平均值

count()    返回某列的行数

max()     返回某列的最大值

min()      返回某列的最小值

sum()      返回某列值之和在终端下输入(先插入一列分数 score 并修改内容)

sqlite> .schema

CREATE TABLEtbl2(id INT, name TEXT , addr TEXT);

CREATE TABLE IF NOT EXISTS "tbl"(id interger primary key ,name text ,addr text,score integer);

sqlite> select*from tbl;

id     name    addr    score

----   --------  -------  ------

101     zs      bj

102     ls       tj

103     ww     sh

104     zs       cd

105     sw      cq

106      xh      hb

/为 score 列填入内容,便于后续操作

sqlite> update tbl set score=40 where id between 101 and 102;

sqlite> update tbl set score=60 where id between 103 and 104;

sqlite> update tbl set score=90 where id between 105 and 106;

sqlite> select*from tbl;

id     name    addr    score

----   --------  -------  ------

101     zs      bj       40

102     ls       tj       40

103     ww     sh      60

104     zs       cd     60

105     sw      cq      90

106      xh      hb     90

接下来求平均值:select avg(score) from…

sqlite> select avg(score) from tbl;

avg(score)

------------

63.3333333

显示 id,name,Addr 没有意义,故不显示。

接下来是返回某列对应的函数:select count (*)from….

sqlite> select count(90) from tbl;

count(90)

--------

6

count(*) 对表中行的数具进行计数

Count 的一个应用:判断数据库中是否有一张表比如判断数据库中是否有 persons 这张表

select count(*) from sqlite_master where type='table' and name="persons";

如果有 persons 这张表,结果返回非 0,没有则返回 0

sqlite_master 是数据库自带的一个表。当用户创建一张表时,数据库会将用户新建的表的信息存放到 sqlite_master 这张表中

count() 的应用比较少,最主要的是返回某列的行数。

求最大值,演示:

sqlite> select max (id) from tbl;

max(id)

--------

106

sqlite> select max ( score) from tbl;

max ( score)

------------

90

sqlite> select id,name , addr , max (score) from tbl;

id     name    addr    score

----   --------  -------  ------

105     sw      cq      90

求最小值,演示:

sqlite> select id,name , addr,min (score) from tbl;

id     name    addr    score

----   --------  -------  ------

101     zs      bj       40

求和,前面的地址没有意义,演示:

sqlite> select id,name , addr , sum( score) from tbl;

id     name    addr    score

----   --------  -------  ------

106     xh       hb     380

以上是结合函数的一些扩充用法。

相关文章
|
4天前
|
SQL 存储 数据挖掘
深入了解SQLite3命令:小巧强大的数据库工具
SQLite3是轻量级数据库工具,适用于嵌入式设备和数据分析。它提供交互式shell,无需服务器,易于部署。常用命令如`.schema`显示表结构,`.mode`设置输出格式。示例包括创建数据库`mydatabase.db`,创建表`users`,插入数据并查询。注意动态类型系统、性能限制及SQL注入安全。适合轻量级数据存储和管理。
|
25天前
|
数据库 Android开发 数据安全/隐私保护
在 Android Studio 中结合使用 SQLite 数据库实现简单的注册和登录功能
在 Android Studio 中结合使用 SQLite 数据库实现简单的注册和登录功能
65 2
|
27天前
|
SQL 关系型数据库 MySQL
MySQL数据库基础第二篇(函数)
MySQL数据库基础第二篇(函数)
|
28天前
|
数据库 数据安全/隐私保护 数据库管理
QT中sqlite数据库数据加密/混淆---MD5/SHA1/SHA2/SHA3
QT中sqlite数据库数据加密/混淆---MD5/SHA1/SHA2/SHA3
|
12天前
|
存储 Java Linux
SQLite3数据库的安装与使用教程
SQLite3数据库的安装与使用教程
|
1月前
|
SQL 关系型数据库 MySQL
MySQL数据库——基础篇总结(概述、SQL、函数、约束、多表查询、事务)一
MySQL数据库——基础篇总结(概述、SQL、函数、约束、多表查询、事务)一
29 5
|
1月前
|
关系型数据库 MySQL 数据库
MySQL数据库——函数-字符串函数、数值函数、日期函数、流程函数
MySQL数据库——函数-字符串函数、数值函数、日期函数、流程函数
18 2
|
17天前
|
编译器 API 数据库
技术好文共享:(xxxx)十一:SQLite3的db数据库解密(三)数据库在线备份
技术好文共享:(xxxx)十一:SQLite3的db数据库解密(三)数据库在线备份
19 0
|
1月前
|
SQL 关系型数据库 数据库
17. Python 数据库操作之MySQL和SQLite实例
17. Python 数据库操作之MySQL和SQLite实例
79 2
|
1月前
|
SQL 存储 数据库
48. 【Android教程】数据库:SQLite 的使用
48. 【Android教程】数据库:SQLite 的使用
22 1