alter pragma 语法 SQLite3 数据库学习(二):SQLite 中的 SQL 语句详解 - Error: near &#34

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力网 👁️ 542 次阅读

参考引用

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|何青德|广州|911683831

sqlite>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|岳飞|中国|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.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|岳飞|中国|199999999999

sqlite>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|岳飞|中国|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.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 # 应该写 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

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

退出表 sqlite>.quit

相关链接

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧