FMDB是用于進(jìn)行數(shù)據(jù)存儲(chǔ)的第三方的框架,它與SQLite與Core Data相比較,存在很多優(yōu)勢(shì)。
FMDB是面向?qū)ο蟮模設(shè)C的方式封裝了SQLite的C語(yǔ)言API,使用起來(lái)更加的方便,不需要過(guò)多的關(guān)心數(shù)據(jù)庫(kù)操作的知識(shí)。但是它本身也存在一些問(wèn)題,比如跨平臺(tái),因?yàn)樗怯胦c的語(yǔ)言封裝的,所以只能在ios開(kāi)發(fā)的時(shí)候使用,如果想實(shí)現(xiàn)跨平臺(tái)的操作,來(lái)降低開(kāi)發(fā)的成本和維護(hù)的成本,就需要使用比較原始的SQLite。
Core Data是ORM的一種體現(xiàn),使用Core Data需要用到模型數(shù)據(jù)的轉(zhuǎn)化,雖然操作簡(jiǎn)單,不需要直接操作數(shù)據(jù)庫(kù),但是性能沒(méi)有直接使用SQLite高。但是SQLite使用的時(shí)候需要使用c語(yǔ)言中的函數(shù),操作比較麻煩,因此需要對(duì)它進(jìn)行封裝。但是如果只是簡(jiǎn)單地封裝,很可能會(huì)忽略很多重要的細(xì)節(jié),比如如何處理并發(fā)以及安全性更問(wèn)題。
因此,在這里推薦使用第三方框架FMDB,它是對(duì)libsqlite3框架的封裝,用起來(lái)的步驟與SQLite使用類似,并且它對(duì)于多線程的同時(shí)操作一個(gè)表格時(shí)進(jìn)行了處理,也就意味著它是線程安全的。FMDB是輕量級(jí)的框架,使用靈活,它是很多企業(yè)開(kāi)發(fā)的首選。
FMDB中重要的類 |
FMDatabase:一個(gè)FMDatabase對(duì)象就代表一個(gè)單獨(dú)的SQLite數(shù)據(jù)庫(kù),用來(lái)執(zhí)行SQL語(yǔ)句
FMResultSet:使用FMDatabase執(zhí)行查詢后的結(jié)果集
FMDatabaseQueue:用于在多線程中執(zhí)行多個(gè)查詢或更新,它是線程安全的
FMDB使用步驟 |
1. 下載FMDB文件 fmdb下載地址 ,將FMDB文件夾添加到項(xiàng)目中
2. 導(dǎo)入sqlite框架,導(dǎo)入FMDatabase.h文件
3.與SQLite使用步驟類似,需要獲取數(shù)據(jù)庫(kù)文件路徑,然后獲得數(shù)據(jù)庫(kù),并打開(kāi)數(shù)據(jù)庫(kù),然后數(shù)據(jù)庫(kù)進(jìn)行操作,最后關(guān)閉數(shù)據(jù)庫(kù)。代碼如下所示:
// ViewController.m// JRFMDB//// Created by jerehedu on 15/6/18.// Copyright (c) 2015年 jerehedu. All rights reserved.//#import "ViewController.h"#import "FMDatabase.h"@interface ViewController ()@PRoperty (nonatomic, strong) FMDatabase *db;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; //導(dǎo)入sqlite框架,導(dǎo)入FMDB文件夾 //1.獲得數(shù)據(jù)庫(kù)文件的路徑 NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *fileName=[doc stringByAppendingPathComponent:@"student.sqlite"]; NSLog(@"fileName = %@",fileName); //2.獲得數(shù)據(jù)庫(kù) FMDatabase *db = [FMDatabase databaseWithPath:fileName]; //3.打開(kāi)數(shù)據(jù)庫(kù) if ([db open]) { NSLog(@"ok"); //4.創(chuàng)表 BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"]; if (result) { NSLog(@"創(chuàng)表成功"); }else{ NSLog(@"創(chuàng)表失敗"); } } self.db=db; //插入數(shù)據(jù) [self insertStu]; [self deleteStu:6]; [self updateStu:@"apple7_name" :@"7777"]; [self queryStu]; [self dropStu]; [self insertStu]; [self queryStu]; //6.關(guān)閉數(shù)據(jù)庫(kù) [self.db close];}#pragma mark 插入數(shù)據(jù)-(void)insertStu{ for (int i=0; i<10; i++) { NSString *name = [NSString stringWithFormat:@"1apple%i_name",i]; int age = arc4random()%3+20; //1. executeUpdate : 不確定的參數(shù)用?來(lái)占位 (后面參數(shù)必須都是oc對(duì)象) [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);",name,@(age)]; //2. executeUpdateWithFormat : 不確定的參數(shù)用%@、%d等來(lái)占位 (參數(shù)為原始數(shù)據(jù)類型) // [self.db executeUpdateWithFormat:@"insert into t_student (name, age) values (%@, %i);",name,age]; //3. 數(shù)組 // [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);" withArgumentsInArray:@[name,@(age)]]; } }#pragma mark 刪除數(shù)據(jù)-(void)deleteStu:(int)idNum{ //a. executeUpdate : 不確定的參數(shù)用?來(lái)占位 (后面參數(shù)必須都是oc對(duì)象) // [self.db executeUpdate:@"delete from t_student where id=?;",@(idNum)]; //b. executeUpdateWithFormat : 不確定的參數(shù)用%@、%d等來(lái)占位 // [self.db executeUpdateWithFormat:@"delete from t_student where name=%@;",@"apple9_name"];}#pragma mark 銷毀表格-(void)dropStu{ [self.db executeUpdate:@"drop table if exists t_student;"]; //4.創(chuàng)表 BOOL result=[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"]; if (result) { NSLog(@"再次創(chuàng)表成功"); }else{ NSLog(@"再次創(chuàng)表失敗"); }}#pragma mark 修改數(shù)據(jù)-(void)updateStu:(NSString *)oldName :(NSString*)newName{ // [self.db executeUpdateWithFormat:@"update t_student set name=%@ where name=%@;",newName,oldName]; [self.db executeUpdate:@"update t_student set name=? where name=?",newName,oldName];}#pragma mark 查詢數(shù)據(jù)-(void)queryStu{ //1.執(zhí)行查詢語(yǔ)句 // FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student;"]; FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student where id<?;",@(14)]; //2.遍歷結(jié)果集合 while ([resultSet next]) { int idNum = [resultSet intForColumn:@"id"]; NSString *name = [resultSet objectForColumnName:@"name"]; int age = [resultSet intForColumn:@"age"]; NSLog(@"id=%i ,name=%@, age=%i",idNum,name,age); } }- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
疑問(wèn)咨詢或技術(shù)交流,請(qǐng)加入官方QQ群: (452379712)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注