iOS数据持久化之数据库:SQLite和FMDB
SQLite:
SQLite是一款轻量级型的数据库,资源占用少、性能良好和零管理成本,具有零配置(无需安装和管理配置)、独立(没有额外依赖)、储存在单一磁盘文件中的一个完整的数据库、源码完全的开源、比一些流行的数据库在大部分普通数据库操作要快……功能特性;在大型系统和处理大批量数据时不适用
SQLite引擎不是程序与之通信的独立进程,而是连接到程序中成为它的一个主要部分,所以主要的通信协议是在编程语言内的直接API调用;在APP开发中将SQLite集成到应用的沙盒目录下(SQLite是以文件形式存在),用于缓存数据到本地,通过Objective-C可以直接调用SQLite的API来访问本地数据
SQLite在iOS开发通常不直接使用,API代码调用很繁琐,因此一般使用第三方框架FMDB或iOS封装的原生的CoreData;使用SQLite需要导入最新SQLite 3.0/SQLite 3框架,而且可以通过代码直接创建SQLite文件和表,需要注意的是数据库每次操作前都需要开库,在每次操作完毕后都需要关闭数据库;如果直接使用SQLite还需要通过FireFox的SQLite Manager创建SQLite文件和表
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAi4AAAA9CAIAAAAF9XWUAAAgAElEQVR4nO3daXAb14EncE9V1lMTJTOJ452yZ6qcZLJVuzvzYXc2s5PZSSbxxE7sWLYTX7KjWJZv67BkybYsWZYs66BO3hLvCyBIgAAJniBxEwABkOAJXiBBEvd930A3ul/32w8gKeq0LOui837FKlFgo/EewOo/X7/rPoggCIIgd9V9d7sACIIgyJ+75SgiEQRBkD8HgCLxZHq4IKU5ntLekS/N8fREJZHNkCS4sjg3GUUAQRAEWb8oCNIBWHofPH0fPHNHvk7dB+sfILEYCahbE0WRSMThcDgRBEGQdcrlctpNbvkJt+gzt/jQHfhyCQ/6taUEfutaRTiOpxEEQZD1LZMG8E5+ZcA1U+ZmouhuNywRBEGQdelWRtFdBACg1rjbxbkZa6twnQ8GQRDkz8d6iiKKonAcj0ajgUAgGAwmEgmSJNfR1Tz3R0E6nQ6Hw4FAIBwOp1Kp6/+lgCAI8udg3UQRACAUCtntdo/H4/f7/X6/y+W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4WLh4M1szF7nLiBxbEQBLlDciOT74Cv3yN166Po5tBr+t2v0ue+ZpxCrmd9dkzb2cHrFA3gF4cyADK3gdUagKIgpEb7e9vEGoKmV8dqr9Q39z0+M6pWaPX42he4tOefXtmw9cqiweuMEbiyHivH5j446uLjF4ck0AAALDHQ19mrHgf0xZpTFLjsbLm37uLYh4slouHKWInc80dFgjF3BF46PuLyOq4dFnG1XyxAkqlM1hzFAU1BQJjC2FIECyRwSxTzYwSEFERb1yIIclNyV6G7H0VfCQAAT8e9Xn+WvIFFzIlsLBrByWvvNLUSMPfQn/NZPBaJZsEtm0ibxfEscQuGWQNAQmp5yD2kAKQATZGQAvfSe4cgyPqzLqOIXFn57QYv1dQ6XB3h1pYZ3Ni+IwiCIHfFeo0iBEEQ5BvjJqMIIAiCIMjV3KEoIggCw7AMgiAIglyB+Oo90zcTRTiO56Y1IQiCIMhlUqnUV20b3XxfEYEgCIIgV/hKUfJ1owhBEARBbgkURQiCIMhddkkUIQiCIMjdgqIIQRAEucv+P+4g9gFHNmXQAAAAAElFTkSuQmCC" alt="" />
FMDB:
12.00
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}
FMDB是对操作SQLite的封装,框架实现了通过代码直接创建表的功能,建表前判断表是否存在,不存在才创建,因此开库操作就是调用建表方法;FMDB将对数据库的操作分为两类:修改(update)和查询(query)
FMDB只是封装了对SQLite的操作,因此在使用时还需要创建数据库管理类:DBManager,其对建表(开库)、操作类型(每次操作完后关库)进行封装;当数据库和模型绑定时,需要在数据库的操作类型中进行数据的模型化操作,操作模型即能实现和数据库的关联,而且每一种操作类型都能被够复用
1. FMDB的下载地址 https://github.com/ccgus/fmdb
2. 在FMDB下载文件后,工程中必须导入如下文件,并使用 libsqlite3.dylib 依赖包
3. FMDB常用类
FMDatabase : 一个单一的SQLite数据库,用于执行SQL语句。
FMResultSet :执行查询一个FMDatabase结果集。
FMDatabaseQueue :在多个线程来执行查询和更新时会使用这个类
对FMDB操作类型的封装,DMManager类封装的对数据的增删改查、指定查询、建表(开库)、模型转换操作
#import "DBMamager.h"
#import "FMDB.h" // 宏定义表的关键字
#define TABNAME @"MEMBER"
#define ID @"MEMID"
#define NAME @"MEMNAME"
#define SEX @"MEMSEX"
#define TEL @"MEMTEL"
#define QQ @"MEMQQ"
#define DB @"member.sqlite" @interface DBMamager () @property (nonatomic, strong)FMDatabase *db;
@property (nonatomic, strong)NSString *pathDB;
@end
@implementation DBMamager // 建表
- (void)createDB
{
_pathDB = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:] stringByAppendingPathComponent:DB];
_db = [FMDatabase databaseWithPath:_pathDB];
if ([_db open])
{
NSString *sql = [NSString stringWithFormat:@"create table if not exists '%@' ('%@' integer primary key autoincrement, '%@' varchar(20), '%@' varchar(10),'%@' varchar(20),'%@' varchar(20))", TABNAME, ID, NAME, SEX, QQ, TEL];
[_db executeUpdate:sql]; }
[_db close];
} // 查询所有
- (NSMutableArray *)getallMember
{
NSMutableArray *stus = [NSMutableArray array]; if ([_db open])
{
NSString *sql = [NSString stringWithFormat:@"select * from %@", TABNAME]; FMResultSet *resultSet = [_db executeQuery:sql]; while (resultSet.next)
{
Member *mem = [[Member alloc] initWithDic:
@{@"memid":@([resultSet intForColumn:ID]),
@"name":[resultSet stringForColumn:NAME],
@"sex":[resultSet stringForColumn:SEX],
@"qq":[resultSet stringForColumn:QQ],
@"tel":[resultSet stringForColumn:TEL]
}]; [stus addObject:mem];
}
}
[_db close];
return stus;
} // 增加
- (BOOL)insertMember:(Member *)mem
{
if ([_db open])
{
NSString *sql = [NSString stringWithFormat:@"insert into %@(%@, %@, %@, %@) values('%@', '%@', '%@', '%@')", TABNAME, NAME, SEX, QQ, TEL, mem.name, mem.sex, mem.qq, mem.tel];
return [_db executeUpdate:sql];
}
[_db close];
return NO;
} // 删除
- (BOOL)deleteMember:(NSInteger)memid
{
if ([_db open])
{
NSString *sql = [NSString stringWithFormat:@"delete from %@ where %@ = %lu", TABNAME, ID, memid];
return [_db executeUpdate:sql];
}
[_db close];
return NO;
} // 修改
- (BOOL)updateMember:(Member *)mem
{
if ([_db open])
{
NSString *sql = [NSString stringWithFormat:@"update %@ set %@ = '%@', %@ = '%@', %@ = '%@', %@ = '%@' where %@ = %lu", TABNAME, NAME, mem.name, SEX, mem.sex, QQ, mem.qq, TEL, mem.tel, ID, mem.memid];
return [_db executeUpdate:sql];
}
[_db close];
return NO;
} // 指定memid查询
- (Member *)selectMember:(NSInteger)memid
{
if ([_db open])
{
NSString *sql = [NSString stringWithFormat:@"select * from %@ where %@ = %lu", TABNAME, ID, memid]; FMResultSet *resultSet = [_db executeQuery:sql];
while (resultSet.next)
{
Member *mem = [[Member alloc] initWithDic:
@{@"memid":@([resultSet intForColumn:ID]),
@"name":[resultSet stringForColumn:NAME],
@"sex":[resultSet stringForColumn:SEX],
@"qq":[resultSet stringForColumn:QQ],
@"tel":[resultSet stringForColumn:TEL]
}];
return mem;
}
}
return nil;
}
@end
4. 如果应用中使用了多线程操作数据库,那么就需要使用FMDatabaseQueue来保证线程安全了。 应用中不可在多个线程中共同使用一个FMDatabase对象操作数据库,这样会引起数据库数据混乱。 为了多线程操作数据库安全,FMDB使用了FMDatabaseQueue,使用FMDatabaseQueue很简单,首先用一个数据库文件地址来初使 化FMDatabaseQueue,然后就可以将一个闭包(block)传入inDatabase方法中。 在闭包中操作数据库,而不直接参与FMDatabase的管理
//2多线程操作
NSString *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
path=[path stringByAppendingPathComponent:@"test.sqlite"]; FMDatabaseQueue * queue=[FMDatabaseQueue databaseQueueWithPath:path];
[queue inDatabase:^(FMDatabase *db) {
NSString * create=@"create table if not exists t_book(id integer,name varchar)";
BOOL c1= [db executeUpdate:create];
if(c1){
NSLog(@"成功");
} }]; [queue inDatabase:^(FMDatabase *db) {
NSString * insertSql=@"insert into t_book(id,name) values(?,?)"; //插入语句1
bool inflag=[db executeUpdate:insertSql,@(2),@"admin"];
if(inflag){
NSLog(@"插入成功");
} }]; [queue inDatabase:^(FMDatabase *db) { FMResultSet * data=[db executeQuery:@" select * from t_book "]; while (data.next) { int ids=[data intForColumn:@"id"];
NSString *name=[data stringForColumn:@"name"];
NSLog(@"%@",name);
NSLog(@"%i",ids);
}
}];
12.00
FMDB是对操作SQLite的封装,框架实现了通过代码直接创建表的功能,建表前判断表是否存在,不存在才创建,因此开库操作就是调用建表方法;FMDB将对数据库的操作分为两类:修改(update)和查询(query)
FMDB只是封装了对SQLite的操作,因此在使用时还需要创建数据库管理类:DBManager,其对建表(开库)、操作类型(每次操作完后关库)进行封装;当数据库和模型绑定时,需要在数据库的操作类型中进行数据的模型化操作,操作模型即能实现和数据库的关联,而且每一种操作类型都能被够复用
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}
iOS数据持久化之数据库:SQLite和FMDB的更多相关文章
- iOS 数据持久化(扩展知识:模糊背景效果和密码保护功能)
本篇随笔除了介绍 iOS 数据持久化知识之外,还贯穿了以下内容: (1)自定义 TableView,结合 block 从 ViewController 中分离出 View,轻 ViewControll ...
- iOS数据持久化方式及class_copyIvarList与class_copyPropertyList的区别
iOS数据持久化方式:plist文件(属性列表)preference(偏好设置)NSKeyedArchiver(归档)SQLite3CoreData沙盒:iOS程序默认情况下只能访问自己的程序目录,这 ...
- iOS开发笔记-swift实现iOS数据持久化之归档NSKeyedArchiver
IOS数据持久化的方式分为三种: 属性列表 (plist.NSUserDefaults) 归档 (NSKeyedArchiver) 数据库 (SQLite.Core Data.第三方类库等 归档(又名 ...
- IOS数据持久化之归档NSKeyedArchiver
IOS数据持久化的方式分为三种: 属性列表 (自定义的Property List .NSUserDefaults) 归档 (NSKeyedArchiver) 数据库 (SQLite.Core Data ...
- iOS -数据持久化方式-以真实项目讲解
前面已经讲解了SQLite,FMDB以及CoreData的基本操作和代码讲解(CoreData也在不断学习中,上篇博客也会不断更新中).本篇我们将讲述在实际开发中,所使用的iOS数据持久化的方式以及怎 ...
- iOS数据持久化--数据库
一.简介 1.iOS常用的5中存储方式 (1)plist (2)preference(用户属性) (3)归档 (4)数据库 (5)core data 其中(1)(2) (3) 都只能存储小型的数据,因 ...
- iOS数据持久化-OC
沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...
- iOS数据持久化
在iOS中,实现数据持久化一般分为4大种: 1.属性列表 2.对象归档 3.SQLite 4.Core Data 一.属性列表 NSUserDefaults类的使用和NSKeyedArchiver有很 ...
- 转载 -- iOS数据持久化存储
作者:@翁呀伟呀 授权本站转载 概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方 ...
随机推荐
- 从Map、JSONObject取不存在键值对时的异常情况
1.在Map中取不存在的键值对时不会报异常,只会返回null. @Test public void testMap() { Map<String, Object> map = new Ha ...
- cookie的简单应用
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- HDU 5800 To My Girlfriend
背包变形.dp[i][j][g][h]表示前i个数字,和为j,有g个必选,有h个必不选的方案数. 答案为sum{dp[n][j][2][2]}*4 #pragma comment(linker, &q ...
- zencart低版本由php5.2.17升级PHP5.3环境下错误及解决方案
方法:有错误或者空白先打开错误提示,ftp看cache错误日志 或者 面板查看错误日志,再对比数据库是否正确,可拿脚本看是否能连接 \cp -r backipmysql/nlbarb_007li/ ...
- <context:annotation-config> 和 <context:component-scan>的区别
转自:GOOD spring <context:annotation-config> 跟 <context:component-scan>诠释及区别 <context:a ...
- CSU 1004并查集
试题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1004 题目分析: 讲述的主要是是否可以通过公交直接到达自己的目的地,如果最后将问题转换为 ...
- Service的启动方式
Service的启动方式: 两种启动模式,一种是非绑定启动模式,另一种是绑定启动模式. 一.startservice方式启动 Intent intent = new Intent(this, Firs ...
- qmake pro工程设置
----我的生活,我的点点滴滴!! #以下是在terminal里面编译,想编译哪个就修改pro文件 #================================================= ...
- C# List<T> To DataTable
public DataTable ConvertToDataTable<T>(IList<T> data) { PropertyDescriptorCollection pro ...
- KVM 基本硬件容量扩容
在工作当中如果虚拟机的容量不够使用 如何添加呢? CPU添加 cpu添加有两种方式: 1 创建虚拟机的时候可以添加 # virt-install --help | grep cpu --vcpus=V ...