参考引用
$ sqlite3
sqlite> .open my.db # 打开 my.db 数据库(不存在则直接创建)
sqlite> create table student(number varchar(256), name varchar(256), address text, QQ char(32));
sqlite> insert into student values('20200101', '张三', '广州','911683830');
sqlite> insert into student values('20200102', '何青德', '广州','911683831');
sqlite> select * from student;
20200101|张三|广州|911683830
20200102|何青德|广州|911683831
sqlite> select name, qq from student;
张三|911683830
何青德|911683831
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
sqlite> update student set qq='199999999999' where name='岳飞';
sqlite> select * from student;
20200101|张三|广州|911683830
20200102|何青德|广州|911683831
20200103|何阳华|北京|10080
20200104|岳飞|中国|199999999999
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
sqlite> .schema student
CREATE TABLE student(number varchar(256), name varchar(256), address text, QQ char(32));
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|男
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
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);
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
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
删除表
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
退出表
sqlite> .quit