iOS數(shù)據(jù)存儲
在項目開發(fā)中法梯,通常都需要對數(shù)據(jù)進(jìn)行離線緩存的處理,離線緩存一般都是把數(shù)據(jù)保存到項目的沙盒中。有以下幾種方式
(1)歸檔:NSCodeing、NSKeyedArchiver
(2)偏好設(shè)置:NSUserDefaults
(3)Plist存儲:writeToFile
上述三種方法都有一個致命的缺點(diǎn)恢恼,那就是都無法存儲大批量的數(shù)據(jù)舱污,有性能的問題
例如使用歸檔時
(1)數(shù)據(jù)的存取都必須是完整的,要求寫入的時候要一次性寫入粱栖,讀取的時候要一次性全部讀取迎捺,這涉及到應(yīng)用的性能問題。
(2)如果有1000條數(shù)據(jù)查排,此時要把第1001條數(shù)據(jù)存入凳枝,那么需要把所有的數(shù)據(jù)取出來,把這條數(shù)據(jù)加上去之后跋核,再存入岖瑰。
所以,以上的三種技術(shù)不能處理大批量數(shù)據(jù)的存儲,大批量數(shù)據(jù)通常使用數(shù)據(jù)庫來進(jìn)行存儲
所以,iOS中數(shù)據(jù)的存儲方式
(1)歸檔:NSCoding(NSKeyedArchiver\NSkeyedUnarchiver)
(2)偏好設(shè)置Preference(NSUserDefaults)
(3)Plist存儲(NSArray\NSDictionary)
(4)SQLite3
(5)Core Data
備注:
3是版本號砂代,是SQLite的第三個版本蹋订。
Core Data是蘋果自帶,對SQLite的封裝
FMDB是第三方,iOS平臺的SQLite數(shù)據(jù)庫框架,FMDB以O(shè)C的方式封裝了SQLite的C語言API
FMDB使用起來更加面向?qū)ο螅∪チ撕芏嗦闊? 對比蘋果自帶的Core Data框架刻伊,更加輕量級和靈活
SQLite介紹
數(shù)據(jù)庫介紹
數(shù)據(jù)庫(Database)是按照數(shù)據(jù)結(jié)構(gòu)來組織露戒、存儲和管理數(shù)據(jù)的倉庫,有兩種類型
1.關(guān)系型數(shù)據(jù)庫(主流常用)
2.對象型數(shù)據(jù)庫(直接把內(nèi)存中的對象塞入到數(shù)據(jù)庫椒功,對比關(guān)系型數(shù)據(jù)庫而言性能不能很好,效率不高)
常用的關(guān)系型數(shù)據(jù)庫有以下:
1.PC端:Oracle智什、MySQL动漾、SQL Server、Access荠锭、DB2旱眯、Sybase
2.嵌入式\移動客戶端:SQLite
###數(shù)據(jù)庫如何存儲數(shù)據(jù)
數(shù)據(jù)庫的存儲結(jié)構(gòu)和excel很像,以表(table)為單位 证九。表由多個字段(列删豺、屬性、column)組成愧怜,表里面的每一行數(shù)據(jù)稱為記錄
###存儲步驟
1)新建一張表(table)
2)添加多個字段(column呀页,列,屬性)
3)添加多行記錄(row拥坛,record赔桌,每行存放多個字段對應(yīng)的值)
SQLite介紹
SQLite是一款輕型的嵌入式數(shù)據(jù)庫,移動端開發(fā)使用的都是SQLite數(shù)據(jù)庫
優(yōu)點(diǎn):
1)它占用資源非常的低渴逻,在嵌入式設(shè)備中疾党,可能只需要幾百K的內(nèi)存就夠了
2)它的處理速度比Mysql、PostgreSQL這兩款著名的數(shù)據(jù)庫都還快
SQL語句
SQL是一種對關(guān)系型數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行定義和操作的語言
使用SQL語言編寫出來的句子\代碼惨奕,就是SQL語句
操作(增刪改查雪位,CRUD)數(shù)據(jù)庫中的數(shù)據(jù),必須使用SQL語句
SQL語句特點(diǎn)
1.不區(qū)分大小寫(比如數(shù)據(jù)庫認(rèn)為user和UsEr是一樣的)
2.每條語句都必須以分號 ; 結(jié)尾
3.數(shù)據(jù)庫中不可以使用關(guān)鍵字來命名表梨撞、字段
基本操作
注意:數(shù)據(jù)庫中的字符串內(nèi)容應(yīng)該用單引號 ’ 括住
###創(chuàng)建表
create table 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;
create table if not exists 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;
舉例:create table t_student (id integer, name text, age integer, score real) ;
關(guān)于字段類型,有以下幾種:但建表時聲明啥類型或者不聲明類型都可以
integer : 整型值
real : 浮點(diǎn)值
text : 文本字符串
blob : 二進(jìn)制數(shù)據(jù)(比如文件)
###刪除表
drop table 表名 ;
drop table if exists 表名 ;
舉例:drop table t_student ;
或
delete from 表名 ;
舉例:delete from t_student ;
上面的示例會將t_student表中所有記錄都刪掉
###插入數(shù)據(jù)
insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;
舉例:insert into t_student (name, age) values (‘mj’, 10) ;
###更新數(shù)據(jù)
update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;
舉例:update t_student set name = ‘jack’, age = 20 ;
上面的示例會將t_student表中所有記錄的name都改為jack雹洗,age都改為20
###刪除某條數(shù)據(jù)
delete from 表名 where 字段名 = ?
###查詢表中所有字段
select * from 表名;
更多條件語句參考:
http://www.cnblogs.com/wendingding/p/3868926.html
FMDB介紹
FMDB有三個主要的類
(1)FMDatabase
一個FMDatabase對象就代表一個單獨(dú)的SQLite數(shù)據(jù)庫
用來執(zhí)行SQL語句
(2)FMResultSet
使用FMDatabase執(zhí)行查詢后的結(jié)果集
(3)FMDatabaseQueue
用于在多線程中執(zhí)行多個查詢或更新,它是線程安全的
打開數(shù)據(jù)庫
FMDatabase *db = [FMDatabase databaseWithPath:path];
或FMDatabase *db = [[FMDatabase alloc] initWithPath:path];
文件路徑有三種情況
(1)具體文件路徑
如果不存在會自動創(chuàng)建
(2)空字符串@""
會在臨時目錄創(chuàng)建一個空的數(shù)據(jù)庫
當(dāng)FMDatabase連接關(guān)閉時卧波,數(shù)據(jù)庫文件也被刪除
(3)nil
會創(chuàng)建一個內(nèi)存中臨時數(shù)據(jù)庫时肿,當(dāng)FMDatabase連接關(guān)閉時,數(shù)據(jù)庫會被銷毀
數(shù)據(jù)庫必須是打開狀態(tài)港粱,才能與之交互
當(dāng)對數(shù)據(jù)庫進(jìn)行查詢和更新操作完成后,必須關(guān)閉數(shù)據(jù)庫
[db open]
[db close];
更新(創(chuàng)建表及增刪改,除查詢外其他操作)
使用executeUpdate:方法執(zhí)行更新
// executeUpdate : 不確定的參數(shù)用?來占位
- (BOOL)executeUpdate:(NSString*)sql
// executeUpdateWithFormat : 不確定的參數(shù)用%@螃成、%d等來占位
- (BOOL)executeUpdateWithFormat:(NSString*)format
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments
- (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments
查詢數(shù)據(jù)
查詢方法
- (FMResultSet *)executeQuery:(NSString*)sql, ...
- (FMResultSet *)executeQueryWithFormat:(NSString*)format, ...
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments
- (FMResultSet *)executeQuery:(NSString *)sql withParameterDictionary:(NSDictionary *)arguments
查詢某表的全部數(shù)據(jù)
執(zhí)行查詢操作后,如果成功會返回一個 FMResultSet 對象查坪,反之會返回 nil
為了遍歷查詢結(jié)果寸宏,需要 while() 循環(huán),然后逐條記錄查看
即使只需要獲取一個數(shù)據(jù)偿曙,也還是必須在訪問查詢結(jié)果前調(diào)用 -[FMResultSet next]
FMResultSet *res = [db executeQuery:@"SELECT * FROM表名"];
while ([res next])
{
取全部數(shù)據(jù)放入數(shù)組中
[dataArray addObject:[res stringForColumn:@"animal_name"]];
取一條數(shù)據(jù)
int totalCount = [s intForColumnIndex:0];
}
一個簡單的例子
-
1.CocoaPods導(dǎo)入FMDB
-
2.設(shè)置數(shù)據(jù)庫單例
本例子是定義了一個動物的表
在別處可以添加動物,刪除動物,獲取動物列表
//.h文件
#import <Foundation/Foundation.h>
@interface DBSingleton : NSObject
+ (instancetype)sharedDBSingleton;
#pragma mark - 對外接口
- (void)addAnimal:(NSString *)str;
- (void)deleteAnimal:(NSString *)str;
- (NSMutableArray *)getAllAnimal;
@end
//.m文件_實(shí)現(xiàn)單例部分
#import "DBSingleton.h"
#import <FMDB.h>
static DBSingleton *_dbSingleton = nil;
@interface DBSingleton()<NSCopying,NSMutableCopying>
{
FMDatabase *_db;
}
@end
@implementation DBSingleton
#pragma mark - 單例
+(instancetype)sharedDBSingleton
{
if (_dbSingleton == nil)
{
_dbSingleton = [[DBSingleton alloc] init];
[_dbSingleton initDBSingleton];
}
return _dbSingleton;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
if (_dbSingleton == nil)
{
_dbSingleton = [super allocWithZone:zone];
}
return _dbSingleton;
}
-(id)copy
{
return self;
}
-(id)mutableCopy
{
return self;
}
-(id)copyWithZone:(NSZone *)zone
{
return self;
}
-(id)mutableCopyWithZone:(NSZone *)zone
{
return self;
}
//#pragma mark - 數(shù)據(jù)庫初始化
-(void)initDBSingleton
{
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"model.sqlite"];
NSLog(@"地址是%@",filePath);
_db = [FMDatabase databaseWithPath:filePath];
[_db open];
// 初始化數(shù)據(jù)表
NSString *personSql = @"CREATE TABLE 'animal' ('animal_name' VARCHAR(255))";
[_db executeUpdate:personSql];
[_db close];
}
//#pragma mark - 對外接口
- (void)addAnimal:(NSString *)str
{
[_db open];
[_db executeUpdate:@"INSERT INTO animal(animal_name)VALUES(?)",str];
[_db close];
}
- (void)deleteAnimal:(NSString *)str
{
[_db open];
[_db executeUpdate:@"DELETE FROM animal WHERE animal_name = ?",str];
[_db close];
}
- (NSMutableArray *)getAllAnimal
{
[_db open];
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
FMResultSet *res = [_db executeQuery:@"SELECT * FROM animal"];
while ([res next])
{
[dataArray addObject:[res stringForColumn:@"animal_name"]];
}
[_db close];
return dataArray;
}
-
3.外部調(diào)用數(shù)據(jù)庫時
#import "DBSingleton.h"
[[DBSingleton sharedDBSingleton] addAnimal:@"jinmao"];
[[DBSingleton sharedDBSingleton] addAnimal:@"hashiqi"];
[[DBSingleton sharedDBSingleton] addAnimal:@"mao"];
NSMutableArray *array = [[DBSingleton sharedDBSingleton] getAllAnimal];
NSLog(@"數(shù)據(jù)庫內(nèi)容有%@",array);
[[DBSingleton sharedDBSingleton] deleteAnimal:@"jinmao"];
NSMutableArray *array1 = [[DBSingleton sharedDBSingleton] getAllAnimal];
NSLog(@"刪除后的數(shù)據(jù)庫內(nèi)容有%@",array1);
-
4.根據(jù)路徑地址看到的數(shù)據(jù)庫表圖為
思路
在[DBSingleton sharedDBSingleton]的時候
1.創(chuàng)建了文件路徑,并且根據(jù)這個路徑創(chuàng)建了db
2.在db中創(chuàng)建了animal表
3.調(diào)用addAnimal時,將傳入的字段插入animal中的animal_name下
4.調(diào)用deleteAnimal時,是刪除animal中的animal_name下的內(nèi)容
5.當(dāng)調(diào)用getAllAnimal時,利用FMResultSet去檢索animal_name下全部內(nèi)容,并放入數(shù)組中