ios關(guān)于數(shù)據(jù)庫(kù)第三方框架FMDB----基本用法概述 1. iOS開(kāi)發(fā)中對(duì)數(shù)據(jù)進(jìn)行本地緩存可謂家常便飯,小數(shù)據(jù)我們用plist文件或者歸檔緩存即可,即簡(jiǎn)單又方便。但對(duì)于一些列表一樣的數(shù)據(jù)(數(shù)據(jù)量比較大)就要用到數(shù)據(jù)庫(kù)了。 2. 關(guān)于數(shù)據(jù)庫(kù),移動(dòng)開(kāi)發(fā)中肯定首選sqlite3。這是一款輕微型的嵌入式數(shù)據(jù)庫(kù),通過(guò)sql語(yǔ)句進(jìn)行“增刪改查”等數(shù)據(jù)操作。只是sqlite,里面公布的api都是一些純c語(yǔ)言的代碼,用起來(lái)繁瑣不堪,極為痛苦。 3. 而FMDB這個(gè)框架,就是對(duì)sqlite用oc語(yǔ)法進(jìn)行了一層封裝,我們到時(shí)使用數(shù)據(jù)庫(kù)時(shí),就可以直接用面向?qū)ο蟮姆绞竭M(jìn)行數(shù)據(jù)操作。 FMDB三個(gè)主要的類(lèi)1.FMDatabase – 表示一個(gè)單獨(dú)的SQLite數(shù)據(jù)庫(kù)。 用來(lái)執(zhí)行SQLite的命令。 2.FMResultSet – 表示FMDatabase執(zhí)行查詢(xún)后結(jié)果集 3.FMDatabaseQueue – 如果你想在多線(xiàn)程中執(zhí)行多個(gè)查詢(xún)或更新,你應(yīng)該使用該類(lèi)。這是線(xiàn)程安全的。 FMDatabase執(zhí)行數(shù)據(jù)庫(kù)操作時(shí)用到的主要方法- (BOOL)executeUpdate:(NSString*)sql, ...; //能執(zhí)行插入數(shù)據(jù)、刪除數(shù)據(jù)、更新數(shù)據(jù)、建表刪表操作。參數(shù):傳入要執(zhí)行的sql語(yǔ)句 - (FMResultSet *)executeQuery:(NSString*)sql, ...; // 查詢(xún)數(shù)據(jù)時(shí)用此方法。參數(shù):傳入要執(zhí)行的sql語(yǔ)句。返回值:查詢(xún)后結(jié)果集 注:其他的幾個(gè)方法不一一例舉,上面兩個(gè)方法用于數(shù)據(jù)庫(kù)操作,足矣! 使用FMDatabase執(zhí)行數(shù)據(jù)庫(kù)操作使用時(shí)導(dǎo)入依賴(lài)庫(kù):libsqlite3.dylib,在需要用的地方導(dǎo)入頭文件:"FMDB.h" 1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 //獲得沙盒數(shù)據(jù)庫(kù)文件路徑,有這個(gè)文件直接獲得,沒(méi)有會(huì)進(jìn)行創(chuàng)建 5 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"student.sqlite"]; 6 7 // NSLog(@"%@",path); 8 9 // 1.創(chuàng)建數(shù)據(jù)庫(kù)實(shí)例對(duì)象 10 self.db = [FMDatabase databaseWithPath:path]; 11 12 // 2.打開(kāi)數(shù)據(jù)庫(kù) 13 if ([self.db open]) { 14 NSLog(@"數(shù)據(jù)庫(kù)打開(kāi)成功"); 15 16 //創(chuàng)建一張學(xué)生表 17 BOOL result = [self.db executeUpdate:@"create table if not exists t_student(id integer primary key autoincrement,name text,age integer);"]; 18 if (result) { 19 NSLog(@"創(chuàng)表成功!"); 20 } 21 22 } 23 24 25 } 26 27 - (IBAction)insert { 28 //插入數(shù)據(jù) 29 for (int i = 0; i < 20; i++) { 30 NSString *name = [NSString stringWithFormat:@"name%d",i]; 31 32 //注:此處sql語(yǔ)句具體的值可以用?替代,相當(dāng)于占位符,后面逗號(hào)隔開(kāi)后,放具體的值,如此可以防止數(shù)據(jù)寫(xiě)死 33 BOOL result =[self.db executeUpdate:@"insert into t_student (name,age) values (?,?);",name,@(i + 20)]; 34 if (result) { 35 NSLog(@"插入數(shù)據(jù)成功!"); 36 } 37 38 } 39 40 } 41 42 - (IBAction)delete { 43 //刪除數(shù)據(jù) 44 45 [self.db executeUpdate:@"delete from t_student where age > ?;",@(30)]; 46 } 47 48 - (IBAction)update { 49 50 //更新數(shù)據(jù) 51 BOOL result = [self.db executeUpdate:@"update t_student set name = ?;",@"葉德雄"]; 52 53 if (result) { 54 NSLog(@"更新數(shù)據(jù)成功!"); 55 } 56 57 } 58 59 - (IBAction)select { 60 61 //查詢(xún)數(shù)據(jù) 62 //返回一個(gè)FMResultSet集合,通過(guò)索引取數(shù)據(jù),即調(diào)用其方法[set next],開(kāi)始沒(méi)有指向數(shù)據(jù),返回no,調(diào)用一次指向下條數(shù)據(jù),當(dāng)最后跳數(shù)據(jù)指向完時(shí),返回no,下面是通過(guò)while循環(huán)遍歷數(shù)據(jù) 63 FMResultSet *set = [self.db executeQuery:@"select *from t_student where age > ?;",@(30)]; 64 while (set.next) { 65 //通過(guò)字段名字取數(shù)據(jù) 66 int ID = [set intForColumn:@"id"]; 67 NSString *name = [set stringForColumn:@"name"]; 68 int age = [set intForColumn:@"age"]; 69 70 NSLog(@"id=%d,name=%@,age=%d",ID,name,age); 71 } 72 }
上面提到過(guò),直接使用FMDatabase線(xiàn)程是不安全的,在多線(xiàn)程進(jìn)行數(shù)據(jù)庫(kù)操作的情況下建議使用FMDatabaseQueue,詳細(xì)用法如下:執(zhí)行數(shù)據(jù)操作時(shí),用到的核心方法是: - (void)inDatabase:(void (^)(FMDatabase *db))block;//block里面?zhèn)骰財(cái)?shù)據(jù)庫(kù)實(shí)例FMDatabase *db,我們用db對(duì)象進(jìn)行增刪改查操作 2 #import "FMDB.h" 3 4 @interface ViewController () 5 6 @property(nonatomic,strong)FMDatabaseQueue *queue; 7 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 14 [super viewDidLoad]; 15 16 //使用數(shù)據(jù)庫(kù)隊(duì)列操作數(shù)據(jù),fmdb線(xiàn)程是不安全的,建議使用FMDatabaseQueue 17 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"sqlite.data"]; 18 //里面已經(jīng)創(chuàng)建了FMDdataBase實(shí)例,數(shù)據(jù)庫(kù)實(shí)例 19 self.queue = [FMDatabaseQueue databaseQueueWithPath:path]; 20 21 //通過(guò)block,拿到FMDatabase *db 22 [self.queue inDatabase:^(FMDatabase *db) { 23 //創(chuàng)表 24 BOOL result = [db executeUpdate:@"create table if not exists t_student(id integer primary key autoincrement,name text,age integer);"]; 25 if (result) { 26 NSLog(@"創(chuàng)表成功"); 27 } 28 }]; 29 30 } 31 32 - (IBAction)insert 33 34 { //通過(guò)block,拿到FMDatabase *db 35 [self.queue inDatabase:^(FMDatabase *db) { 36 for (int i = 0; i<40; i++) { 37 NSString *name = [NSString stringWithFormat:@"rose-%d", arc4random() % 1000]; 38 NSNumber *age = @(arc4random() % 100 + 1); 39 [db executeUpdate:@"insert into t_student (name, age) values (?, ?);", name, age]; 40 } 41 }]; 42 } 43 44 - (IBAction)update 45 { 46 [self.queue inDatabase:^(FMDatabase *db) { 47 51 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 52 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 53 54 55 - (IBAction)delete 71 {73 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 74 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 75 76} 82 83 - (IBAction)query 84 { 85 [self.queue inDatabase:^(FMDatabase *db) { 86 // 1.查詢(xún)數(shù)據(jù) 87 FMResultSet *rs = [db executeQuery:@"select * from t_student where age > ?;", @50]; 88 89 // 2.遍歷結(jié)果集 90 while (rs.next) { 91 int ID = [rs intForColumn:@"id"]; 92 NSString *name = [rs stringForColumn:@"name"]; 93 int age = [rs intForColumn:@"age"]; 94 95 NSLog(@"%d %@ %d", ID, name, age); 96 } 97 }]; 98 }
1
0
(請(qǐng)您對(duì)文章做出評(píng)價(jià))
|
|
來(lái)自: quasiceo > 《待分類(lèi)1》