当前位置:Gxlcms > 数据库问题 > SQLite3深入浅出

SQLite3深入浅出

时间:2021-07-01 10:21:17 帮助过:35人阅读

文章目录:

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,如图:

技术分享
安装MesaSQLite

接下来我们多插入几条记录,以便演示操作:

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语句以后,我们的数据表中的数据应该是这样的:

技术分享
book


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表中的数据应该是这样的:

技术分享
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 **ppDb                 /* OUT: SQLite db handle */

);

在操作数据库之前,首先要打开数据库。这个函数打开一个sqlite数据库文件,并且返回一个数据库连接对象。假如这个要被打开的数据文件不存在,则一个同名的数据库文件将被创建。如果使用sqlite3_open和sqlite3_open_v2的话,数据库将采用UTF-8的编码方式,sqlite3_open16采用UTF-16的编码方式。如果sqlite数据库被成功打开(或创建),将会返回SQLITE_OK,否则将会返回错误码。

filename:需要被打开的数据库文件的文件名,在sqlite3_open和sqlite3_open_v2中这个参数采用UTF-8编码,而在sqlite3_open16中则采用UTF-16编码。

ppDb:一个数据库连接句柄被返回到这个参数,即使发生错误。唯一的异常是如果sqlite不能分配内存来存放sqlite对象,ppDb将会被返回一个NULL值。

人气教程排行