当前位置: 首页 > news >正文

SQLite3 数据库学习(二):SQLite 中的 SQL 语句详解

参考引用

  • SQLite 权威指南(第二版)
  • SQLite3 入门

1. SQL 语句操作 SQLite 数据库

1.1 创建数据表格

  • create table 表名(字段名 数据类型, 字段名 数据类型, 字段名 数据类型, 字段名 数据类型);
    • 命令行语句结束要加分号 ;
    • . 开头的命令是 SQLite 数据库自带命令,而非 SQL 语句
    $ sqlite3
    sqlite> .open my.db   # 打开 my.db 数据库(不存在则直接创建)
    sqlite> create table student(number varchar(256), name varchar(256), address text, QQ char(32));
    

1.2 插入数据

  • insert into 表名 values(‘字段数据’,‘字段数据’,‘字段数据’,‘字段数据’ );
    • 如果数据类型是 char, varchar, text 数据必须用 ‘’ 或者 “” 引用,建议用 ‘’
    sqlite> insert into student values('20200101', '张三', '广州','911683830');
    sqlite> insert into student values('20200102', '何青德', '广州','911683831');
    

1.3 查询数据

  • select 字段名…字段名 from 表名;
    • 说明:字段名如果是多个可以用逗号隔开,如果是所有可以用星号 *
    sqlite> select * from student;
    20200101|张三|广州|911683830
    20200102|何青德|广州|911683831sqlite> select name, qq from student;
    张三|911683830
    何青德|911683831
    
  • select 字段名…字段名 from 表名 where 条件;
    sqlite> insert into student values('20200103', '何阳华', '北京','10080');
    sqlite> insert into student values('20200104', '岳飞', '中国','1000000000');
    sqlite> select * from student;
    20200101|张三|广州|911683830
    20200102|何青德|广州|911683831
    20200103|何阳华|北京|10080
    20200104|岳飞|中国|1000000000sqlite> select * from student where address='广州';
    20200101|张三|广州|911683830
    20200102|何青德|广州|911683831sqlite> select * from student where address like '广%';  # 模糊查询
    20200101|张三|广州|911683830
    20200102|何青德|广州|911683831# 两个条件同时成立(与)-- and
    sqlite> select * from student where address like '广%' and QQ like '%1';
    20200102|何青德|广州|911683831# 两个条件只要成立一个(或)-- or
    sqlite> select * from student where address like '广%' or QQ like '%1';
    20200101|张三|广州|911683830
    20200102|何青德|广州|911683831
    

1.4 更新数据

  • update 表名 set 字段1=字段1值, 字段2=字段2值… where 条件表达式
    sqlite> update student set qq='199999999999' where name='岳飞';
    sqlite> select * from student;
    20200101|张三|广州|911683830
    20200102|何青德|广州|911683831
    20200103|何阳华|北京|10080
    20200104|岳飞|中国|199999999999
    

1.5 删除数据

  • delete from 表名; // 删除整个表数据,不会删除表格
  • drop table 表名; // 整个表格全部删除–把表格从数据库中也删除
  • delete from 表名 where 条件;
    sqlite> select * from student;
    20200101|张三|广州|911683830
    20200102|何青德|广州|911683831
    20200103|何阳华|北京|10080
    20200104|岳飞|中国|199999999999sqlite> delete from student where number='20200103';
    sqlite> select * from student;
    20200101|张三|广州|911683830
    20200102|何青德|广州|911683831
    20200104|岳飞|中国|199999999999
    

1.6 查询创建表命令

sqlite> .schema student
CREATE TABLE student(number varchar(256), name varchar(256), address text, QQ char(32));

1.7 alter 添加字段

sqlite> select * from student;
20200101|张三|广州|911683830
20200102|何青德|广州|911683831
20200104|岳飞|中国|199999999999sqlite> alter table student add column age int ;
sqlite> select * from student;
20200101|张三|广州|911683830|
20200102|何青德|广州|911683831|
20200104|岳飞|中国|199999999999|sqlite> update student set age=18;
sqlite> select * from student;
20200101|张三|广州|911683830|18
20200102|何青德|广州|911683831|18
20200104|岳飞|中国|199999999999|18sqlite> alter table student add column sex varchar(8) default '男' ;
sqlite> select * from student;
20200101|张三|广州|911683830|18|男
20200102|何青德|广州|911683831|18|男
20200104|岳飞|中国|199999999999|18|

1.8 pragma 查询表结构信息

0|number|varchar(256)|0||0
1|name|varchar(256)|0||0
2|address|text|0||0
3|QQ|char(32)|0||0
4|age|int|0||0
5|sex|varchar(8)|0|'男'|0

2. SQLite 创建带约束条件表格

  • id 自动增长:ID INTEGER PRIMARY KEY AUTOINCREMENT

  • PRIMARY KEY:主键,not null:不能为 NULL,UNIQUE 唯一 ,DEFAULT 默认值

    # 设置 id 为主键,自增加
    # 设置 name 唯一
    # 设置 status 不能为空-默认为值 0
    # 设置 online 不能为空
    create table device(id integer primary key autoincrement, name varchar(256) unique,  status int not NULL default 0, online int not NULL);
    
  • if not exists 判断表格是否存在

    • 如果不存在就创建
    create table if not exists device(id integer primary key autoincrement, name varchar(256) unique, status int default 0, online int not NULL);
    

2.1 插入数据

sqlite> insert into device value(0,'led',0,0);
Error: near "value": syntax error              # 应该写 valuessqlite> insert into device values(0,'led',0,0);
sqlite> insert into device values(0,'led',0,0);
Error: UNIQUE constraint failed: device.id     # id 不能重复sqlite> insert into device values(1,'led',0,0);
Error: UNIQUE constraint failed: device.name   # name 不能重复
sqlite> insert into device values(1,'led1',0,0);sqlite> select * from device;
0|led|0|0
1|led1|0|0
  • 指定字段(列)插入
    • 没有指定的就可以用默认值
    sqlite> insert into device(name, online) values('led2',0);
    sqlite> insert into device(name, online) values('led3',0);
    sqlite> select * from device;
    0|led|0|0
    1|led1|0|0
    2|led2|0|0
    3|led3|0|0
    

2.2 删除、退出表

  • 删除表

    • drop table 表名;
    sqlite> .tables
    device   studentsqlite> create table test(id int);
    sqlite> .tables
    device   student  testsqlite> delete from test;
    sqlite> .tables
    device   student  testsqlite> drop table test;
    sqlite> .tables
    device   student
    
  • 退出表

    sqlite> .quit
    
http://www.lryc.cn/news/230882.html

相关文章:

  • 基础课4——客服中心管理者面临的挑战
  • RFID技术在危险废物管理中的应用解决方案
  • 二百零三、Flume——Flume实时采集数据频率为1s的高频率Kafka数据直接写入ODS层表的HDFS文件路径下
  • Word或者WPS批量调整文中图片大小的快捷方法
  • url在api测试工具可以访问,但在浏览器不能访问
  • k8s之Helm
  • ElasticSearch 增删改查操作
  • ctfshow sql171-179
  • Mysql 自带分页异常
  • MySQL MVCC机制详解
  • 搭建成功simulink-stm32硬件在环开发环境
  • 【计算机网络】UDP协议
  • ubuntu安装mysql8.0.35过程和报错处理
  • SQL基础理论篇(一):什么是SQL
  • 物联网AI MicroPython学习之语法 GPIO输入输出模块
  • phalcon 访问IndexController 中只能访问indexAction方法,访问不了testAction等其它问题的解决办法
  • docker安装AWVS 23.9.231005181
  • 数据同步工具调研选型:SeaTunnel 与 DataX 、Sqoop、Flume、Flink CDC 对比
  • 【Vue】Vue3 Swiper 插件 loop 无限滚动、并且暂停的问题
  • MySQL的DATE_FORMAT函数使用
  • MySQL的SQL预编译及防SQL注入
  • 博流BL602芯片 - 烧录配置
  • websocket实现实时数据推送,发布订阅重连单点登录功能
  • 前端代理模式之【策略模式】
  • 人工智能-深度学习之残差网络(ResNet)
  • arm2 day6
  • RxSwift和Combine的相同点和使用例子
  • [Linux打怪升级之路]-信号的保存和递达
  • 【科研新手指南3】chatgpt辅助论文优化表达
  • 在应用内维护域名缓存时遇到的问题