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


    参考引用

    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
      • 3

    1.2 插入数据

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

    1.3 查询数据

    • select 字段名…字段名 from 表名;
      • 说明:字段名如果是多个可以用逗号隔开,如果是所有可以用星号 *
      sqlite> select * from student;
      20200101|张三|广州|911683830
      20200102|何青德|广州|911683831
      
      sqlite> select name, qq from student;
      张三|911683830
      何青德|911683831
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    • 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|岳飞|中国|1000000000
      
      sqlite> select * from student where address='广州';
      20200101|张三|广州|911683830
      20200102|何青德|广州|911683831
      
      sqlite> 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
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24

    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
      • 2
      • 3
      • 4
      • 5
      • 6

    1.5 删除数据

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

    1.6 查询创建表命令

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

    1.7 alter 添加字段

    sqlite> select * from student;
    20200101|张三|广州|911683830
    20200102|何青德|广州|911683831
    20200104|岳飞|中国|199999999999
    
    sqlite> 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|18
    
    sqlite> alter table student add column sex varchar(8) default '男' ;
    sqlite> select * from student;
    20200101|张三|广州|911683830|18|男
    20200102|何青德|广州|911683831|18|男
    20200104|岳飞|中国|199999999999|18|
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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);
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    • 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);
      
      • 1
      • 2
      • 3
      • 4

    2.1 插入数据

    sqlite> insert into device value(0,'led',0,0);
    Error: near "value": syntax error              # 应该写 values
    
    sqlite> 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 指定字段(列)插入
      • 没有指定的就可以用默认值
      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
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

    2.2 删除、退出表

    • 删除表

      • drop table 表名;
      sqlite> .tables
      device   student
      
      sqlite> create table test(id int);
      sqlite> .tables
      device   student  test
      
      sqlite> delete from test;
      sqlite> .tables
      device   student  test
      
      sqlite> drop table test;
      sqlite> .tables
      device   student
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    • 退出表

      sqlite> .quit
      
      • 1
  • 相关阅读:
    《WebGIS快速开发教程第四版》重磅更新
    【Hadoop】Hadoop概述与核心组件
    Dockerfile自定义镜像实操【镜像结构、Dockerfile语法、构建Java项目】
    git 查看当前版本号
    请求转发 [JavaWeb][Servlet]
    【附源码】计算机毕业设计JAVA医院住院部管理
    研发主管接私活被辞退,法院判决公司赔偿20.7万元
    Python学习 -- 正则表达式(re模块)
    设计模式之命令模式
    GPIO 模拟SPI
  • 原文地址:https://blog.csdn.net/qq_42994487/article/details/134401587