IOS 資料存放區之 FMDB 詳解,iosfmdb
FMDB是用於進行資料存放區的第三方的架構,它與SQLite與Core Data相比較,存在很多優勢。
FMDB是物件導向的,它以OC的方式封裝了SQLite的C語言API,使用起來更加的方便,不需要過多的關心資料庫操作的知識。但是它本身也存在一些問題,比如跨平台,因為它是用oc的語言封裝的,所以只能在ios開發的時候使用,如果想實現跨平台的操作,來降低開發的成本和維護的成本,就需要使用比較原始的SQLite。
Core Data是ORM的一種體現,使用Core Data需要用到模型資料的轉化,雖然操作簡單,不需要直接操作資料庫,但是效能沒有直接使用SQLite高。但是SQLite使用的時候需要使用c語言中的函數,操作比較麻煩,因此需要對它進行封裝。但是如果只是簡單地封裝,很可能會忽略很多重要的細節,比如如何處理並發以及安全性更問題。
因此,在這裡推薦使用第三方架構FMDB,它是對libsqlite3架構的封裝,用起來的步驟與SQLite使用類似,並且它對於多線程的同時操作一個表格時進行了處理,也就意味著它是安全執行緒的。FMDB是輕量級的架構,使用靈活,它是很多企業開發的首選。
FMDatabase:一個FMDatabase對象就代表一個單獨的SQLite資料庫,用來執行SQL語句
FMResultSet:使用FMDatabase執行查詢後的結果集
FMDatabaseQueue:用於在多線程中執行多個查詢或更新,它是安全執行緒的
1. 下載FMDB檔案 fmdb ,將FMDB檔案夾添加到項目中
2. 匯入sqlite架構,匯入FMDatabase.h檔案
3.與SQLite使用步驟類似,需要擷取資料庫檔案路徑,然後獲得資料庫,並開啟資料庫,然後資料庫進行操作,最後關閉資料庫。代碼如下所示:
// 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]; //匯入sqlite架構,匯入FMDB檔案夾 //1.獲得資料庫檔案的路徑 NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *fileName=[doc stringByAppendingPathComponent:@"student.sqlite"]; NSLog(@"fileName = %@",fileName); //2.獲得資料庫 FMDatabase *db = [FMDatabase databaseWithPath:fileName]; //3.開啟資料庫 if ([db open]) { NSLog(@"ok"); //4.創表 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(@"創表成功"); }else{ NSLog(@"創表失敗"); } } self.db=db; //插入資料 [self insertStu]; [self deleteStu:6]; [self updateStu:@"apple7_name" :@"7777"]; [self queryStu]; [self dropStu]; [self insertStu]; [self queryStu]; //6.關閉資料庫 [self.db close];}#pragma mark 插入資料-(void)insertStu{ for (int i=0; i<10; i++) { NSString *name = [NSString stringWithFormat:@"1apple%i_name",i]; int age = arc4random()%3+20; //1. executeUpdate : 不確定的參數用?來佔位 (後面參數必須都是oc對象) [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);",name,@(age)]; //2. executeUpdateWithFormat : 不確定的參數用%@、%d等來佔位 (參數為未經處理資料類型) // [self.db executeUpdateWithFormat:@"insert into t_student (name, age) values (%@, %i);",name,age]; //3. 數組 // [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);" withArgumentsInArray:@[name,@(age)]]; } }#pragma mark 刪除資料-(void)deleteStu:(int)idNum{ //a. executeUpdate : 不確定的參數用?來佔位 (後面參數必須都是oc對象) // [self.db executeUpdate:@"delete from t_student where id=?;",@(idNum)]; //b. executeUpdateWithFormat : 不確定的參數用%@、%d等來佔位 // [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.創表 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(@"再次創表成功"); }else{ NSLog(@"再次創表失敗"); }}#pragma mark 修改資料-(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 查詢資料-(void)queryStu{ //1.執行查詢語句 // FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student;"]; FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student where id<?;",@(14)]; //2.遍曆結果集合 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
疑問諮詢或技術交流,請加入官方QQ群: (452379712)
作者:傑瑞教育
出處:http://www.cnblogs.com/jerehedu/
本文著作權歸煙台傑瑞教育科技有限公司和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連,否則保留追究法律責任的權利。