一、SQLite3 数据库安装
1、本地安装
sudo dpkg -i *.deb
2、在线安装
sudo apt install sqlite3
3、检查是否安装完成
sqlite3 -version
二、SQLite3 基本命令
1、系统命令
以 “.” 开头的命令
.help 帮助
.quit 退出
.exit 退出
. schema 查看表的结构图
.databases 查看当前系统打开的数据库
.table 查看当前数据库的表
2、sql命令
基本的sql命令,不以 “.” 开头,但都要以 “;” 结尾
创建一张数据库表 stu
// 不带主键
create table stu(id Integer, name char, score Integer);
// 带主键
create table stu(id Integer primary key, name char, score Integer);
插入一条记录
// 完整插入
insert into stu values(1001, 'zhangsan', 80);
// 部分插入
insert into stu(id,name) values(1002,"lisi");
查询记录
// 查询所有字段的结果
select * from stu;
// 查询数据库中的部分字段的内容
select name,score form stu
// 条件查询
select * from stu where score=80;
select * from stu where score=80 and name='zhangsan';
select * from stu where score=80 ro name='lisi';
删除记录
// 删除一整个表记录
delete from stu
// 删除一条记录
delete from stu where name='lisi';
更新记录
// 单字段更改
update stu set name='wangwu' where id=1001;
// 多字段更改
update stu set name='zhaoliu',score=100 where id 1001;
插入一列
alter table stu add column address char;
删除一列
// SQLite3 不支持直接删除一列
// 1. 创建一张新表
create table stu1 as select id,name,score from stu;
// 2. 删除原有的表
drop table stu;
// 3. 将新表名字改成原有旧表名字
alter table stu1 rename to stu;