一.Sqlite 必备知识点
1.基本概念
(1)Sqlite是什么?
Sqlite是一款轻型的数据库,现在最新版本为Sqlite3.
(2)Sqlite优点
Sqlite是开源的,它采用C语言编写,具有可移植性强,可靠性高,小而容易使用的特点.
在存储和检索大量数据方面非常有效,它还能够对数据进行复杂的聚合,与NSUserDefaults,数据归档等方式相比,获取结果的速度更快
2.Sqlite数据类型
(1)INTEGER
有符号的整数类型
(2)REAL
浮点类型
(3)TEXT
字符串类型,采用UTF-8和UTF-16字符编码
(4)BLOB
二进制大对象类型,能够存放任何二进制数据
(5)说明
- Sqlite只有以上四种数据类型.
- Sqlite中没有Boolean类型,可以用整数0和1替代.
- Sqlite中没有日期和时间类型,它们存储在TEXT,REAL,INTEGER类型中
3.关键词
(1)主键
每个表只能有一个,修饰的字段值不能重复,不能为空
例:
(uid integer primary key autoincrement,name,score)
(2)唯一键
可以有多个,修饰的字段值不能重复,可以为空
例:
(uid integer unique,name,score)
(3)外键
和其他表相关联的字段
二.终端下使用Sqlite
- 每条sql语句必须以
;
结尾,注意是sql语句,而不是终端sqlite命令语句 - 以下sqlite命令语句用绿色代码来区分,这类代码后面不需要添加
;
(1)启动sqlite程序
sqlite3 数据库名.db
程序举例:
打开database.db数据库文件,如果文件不存在,则创建
sqlite3 database.db
(2)退出sqlite程序
.quit
(3)查看表
.table
(4)创建表
①create table 表名(字段1,字段2,...);
②create table if not exists 表名(字段1,字段2,...);
程序举例:
//1.
create table USER(id,name,score);
//2.
create table if not exists USER(id integer primary key auto increment,name,score);
(5)删除表
①drop table 表名;
②drop table if exists 表名;
程序举例:
drop table USER;
(6)增
insert into 表名(字段1,字段2,...) values(值1,值2,...);
程序举例:
insert into USER(id,name,score) values(1,"Story5",95);
(7)删
delete from 表名 where 条件;
程序举例:
delete from USER where id=1;
(8)改
update 表名 set 字段=新值 where 条件;
程序举例:
update USER set score = 99 where id = 1;
(9)查
①查询所有记录的所有字段
select * from USER;
②查询前n条记录
select * from USER limit n;
③查询某些字段
select 字段1,字段2 from 表名;
④排序查询
select 字段1,字段2 from 表名 order by 字段 asc/desc;
⑤多表查询
select USER.name,KUNGFU.name,USER.score from USER,KUNGFU where USER.id = KUNGFU.id;
⑥其他查询
1⃣️查询记录个数
select count(*) from 表名;
2⃣️查询字段值总和
select num(字段) from 表名;
3⃣️查询字段平均数
select avg(字段) from 表名;
4⃣️查询字段最值
A.查询字段最大值
select max(字段) from 表名;
B.查询字段最小值
select min(字段) from 表名;
三.Xcode下使用FMDB第三方库操作sqlite
1.导入系统支持库
Build Phases —> Link Binary With Libraries —> Add —> 导入libsqlite3.tbd系统库
2.沙盒路径下实例化fmdb
NSString *sandBoxFilePath = [NSString stringWithFormat:@"%@/Documents/abc.db", NSHomeDirectory()];
FMDatabase *fmdb = [[FMDatabase alloc] initWithPath:sandBoxFilePath];
3.打开sqlite数据库
BOOL res = [fmdb open];
if (res == NO) {
NSLog(@"数据库打开失败");
}
4.创建表
res = [fmdb executeUpdate:@"create table if not exists USER(uid integer primary key autoincrement,name,age,image)"];
if (res == NO) {
NSLog(@"创建表失败");
}
5.增
数据库只能存储NSString
,NSNumber
,NSData
类
int age = [_ageField.text intValue];
NSData* data = UIImagePNGRepresentation(_imageView.image);
BOOL res = [fmdb executeUpdate:@"insert into USER(name,age,image) values(?,?,?)", _nameField.text, [NSNumber numberWithInt:age], data];
if (res == NO) {
NSLog(@"添加用户失败");
} else {
NSLog(@"添加用户成功");
}
6.删
BOOL res = [fmdb executeUpdate:@"delete from USER where name=?", _nameField.text];
if (res) {
NSLog(@"删除成功");
}
7.查
/*
uid name age
1 李寻欢 38
2 李不欢 58
3 李亚鹏 48
*/
FMResultSet *set = [fmdb executeQuery:@"select * from USER"];
while ([set next]) {
//取出每一个字段的值
NSString* name = [set stringForColumn:@"name"];
int age = [set intForColumn:@"age"];
NSData* data = [set dataForColumn:@"image"];
NSLog(@"%@-%d", name, age);
}