如何组建网站开发团队,周至做网站,江苏城市建设职业学院网站,中国建设银行个人查询参考引用 SQLite 权威指南#xff08;第二版#xff09;SQLite3 入门 1. SQL 语句操作 SQLite 数据库
1.1 创建数据表格
create table 表名(字段名 数据类型#xff0c; 字段名 数据类型#xff0c; 字段名 数据类型#xff0c; 字段名 数据类型); 命令行语句结束要加分… 参考引用 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
何青德|911683831select 字段名…字段名 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|何青德|广州|9116838311.4 更新数据
update 表名 set 字段1字段1值, 字段2字段2值… where 条件表达式sqlite update student set qq199999999999 where name岳飞;
sqlite select * from student;
20200101|张三|广州|911683830
20200102|何青德|广州|911683831
20200103|何阳华|北京|10080
20200104|岳飞|中国|1999999999991.5 删除数据
delete from 表名 // 删除整个表数据不会删除表格drop table 表名 // 整个表格全部删除–把表格从数据库中也删除delete from 表名 where 条件sqlite select * from student;
20200101|张三|广州|911683830
20200102|何青德|广州|911683831
20200103|何阳华|北京|10080
20200104|岳飞|中国|199999999999sqlite delete from student where number20200103;
sqlite select * from student;
20200101|张三|广州|911683830
20200102|何青德|广州|911683831
20200104|岳飞|中国|1999999999991.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 age18;
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|男|02. SQLite 创建带约束条件表格 id 自动增长ID INTEGER PRIMARY KEY AUTOINCREMENT PRIMARY KEY主键not null不能为 NULLUNIQUE 唯一 DEFAULT 默认值 # 设置 id 为主键自增加
# 设置 name 唯一
# 设置 status 不能为空-默认为值 0
# 设置 online 不能为空
create table deviceid 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|02.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