SQLite3深入浅出
Posted 戴尼玛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQLite3深入浅出相关的知识,希望对你有一定的参考价值。
文章目录:
1、sqlite3 基础语句
2、sqlite3 API
3、sqlite3 线程安全
4、FMDB
1、基础语句:
学习sqlite3的基础在于SQL语句,开始前请输入$ sqlite3 验证你的电脑是否已经安装了sqlite3
首先我们需要创建一个数据库文件,打开终端,在合适的目录下,输入:
$ sqlite3 studyDB.db
.database
1.1、创建表
以id为主键并自动增加,创建一个名为book的表:
create table if not exists book (id integer primary key autoincrement, bookNumber integer, bookName text, authorID integer, pressName text);
输入如下命令查看数据表是否创建成功:
.tables
1.2、insert
有了数据表以后,就可以愉快地写SQL语句来测试了,先从基础的增删改查开始:
输入如下指令,向数据表中插入一条记录:
insert into book (bookNumber, bookName, authorID, pressName) values (1001, ‘三国演义‘, 10, ‘长江出版社‘);
1.3、select
然后做查询操作,看上一条记录是否插入成功:
select * from book;
然而在终端练习SQL语句,看起来并不那么清晰,所以接下来我们用一个可视化工具MesaSQLite来练习SQL语句,解压之后请阅读Serial.txt,如图:
接下来我们多插入几条记录,以便演示操作:
insert into book (bookNumber, bookName, authorID, pressName) values (1002, ‘水浒传‘, 11, ‘黄河出版社‘);
insert into book (bookNumber, bookName, authorID, pressName) values (1003, ‘西游记‘, 12, ‘长沙出版社‘);
insert into book (bookNumber, bookName, authorID, pressName) values (1004, ‘红楼梦‘, 13, ‘武汉出版社‘);
insert into book (bookNumber, bookName, authorID, pressName) values (1005, ‘琅琊榜‘, 14, ‘黄河出版社‘);
insert into book (bookNumber, bookName, authorID, pressName) values (1006, ‘伪装者‘, 15, ‘长江出版社‘);
insert into book (bookNumber, bookName, authorID, pressName) values (1007, ‘简爱‘, 16, ‘长江出版社‘);
insert into book (bookNumber, bookName, authorID, pressName) values (1008, ‘大主宰‘, 14, ‘武汉出版社‘);
执行以上SQL语句以后,我们的数据表中的数据应该是这样的:
1.4、where
条件语句where,查询bookNumber为1003的记录:
select * from book where bookNumber = 1003;
1.5、update
修改bookNumber为1002的记录,然后查询所有记录:
update book set pressName = ‘清华大学出版社‘ where bookNumber = 1002;
select * from book;
1.6、delete
删除红楼梦,然后查询:
delete from book where bookName = ‘红楼梦‘;
select * from book;
1.7、and
逻辑运算符and,查询pressName为黄河出版社,并且authorID为14的记录:
select * from book where pressName = ‘黄河出版社‘ and authorID = 14;
1.8、or
逻辑运算符or,查询authorID为14,或者pressName为长江出版社的记录:
select* from book where pressName= ‘长江出版社‘ or authorID = 14;
1.9、like
模糊查询指令like:
select * from book where pressName like ‘长%‘;
select * from book where authorID like ‘_4‘;
1.10、in
查询authorID为14或16的记录:
select * from book where authorID in (14, 16);
1.11、not in
查询pressName不为“长江出版社”的记录:
select * from book where pressName not in (‘长江出版社‘);
1.12、between
查询authorID在14到20之间的记录:
select * from book where authorID between 14 and 20;
1.13、count
查询pressName为“长江出版社”的记录条数:
select count(pressName) from book where pressName = ‘长江出版社‘;
为了方便演示,我们创建另一个数据表author:
create table if not exists author (id integer primary key autoincrement, authorName text, authorID integer, age integer);
执行如下SQL语句:
insert into author (authorName, authorID, age) values (‘jack‘, 21, 45);
insert into author (authorName, authorID, age) values (‘dave‘, 10, 33);
insert into author (authorName, authorID, age) values (‘rose‘, 14, 24);
insert into author (authorName, authorID, age) values (‘jim‘, 16, 56);
insert into author (authorName, authorID, age) values (‘ivan‘, 13, 22);
最后我们的author表中的数据应该是这样的:
1.14、sum
查询所有作者的年龄总和:
select sum(age) from author;
1.15、avg
查询作者的平均年龄:
select avg(age) from author;
1.16、max
查询最大的作者年龄:
select max(age) from author;
1.17、min
查询最小的作者年龄:
select min(age) from author;
1.18、order by
查询所有的记录,并按年龄升序排列:
select * from author order by age asc;
查询所有的记录,并按年龄降序排列:
select * from author order by age desc;
1.19、语句嵌套
查询年龄小于平均年龄的记录:
select * from author where age < (select avg(age) from author);
1.20、多表联合查询
查询年龄小于平均年龄的作者姓名、图书名、出版社:
select author.authorName, book.bookName, book.pressName from author, book where author.authorID = book.authorID and age< (select avg(age) from author);
2、sqlite3 API
要使用sqlite3 API,需要导入libsqlite3.tbd,然后#import 就可以使用sqlite了。
使用的过程根据使用的函数大致分为如下几个过程:
sqlite3_open()
sqlite3_prepare()
sqlite3_step()
sqlite3_column()
sqlite3_finalize()
sqlite3_close()
这几个过程是概念上的说法,而不完全是程序运行的过程,如sqlite3_column()表示的是对查询获得一行里面的数据的列的各个操作统称,实际上在sqlite中并不存在这个函数。
2.1、sqlite3_open
函数定义:
SQLITE_API int SQLITE_STDCALL sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3
以上是关于SQLite3深入浅出的主要内容,如果未能解决你的问题,请参考以下文章