FMDB
- 第三方的框架.它是對 libsqlite2框架的封裝.使用方法與 sqlite 類似.并且它是對于多線程的并發(fā)操作進行了處理,所以線程是安全的.是一款簡潔/易用的封裝庫.
優(yōu)點
- 1,對多線程的并發(fā)操作進行處理,所以是線程安全的.
- 2,以 OC 的方式封裝了 SQLITE 的c 語言 API, 使用起來更加的方便.
- 3,FMDB 是輕量級的框架.使用靈活.
缺點
- 只能限制在 IOS 開發(fā)的時候使用.
FMDB 中重要的類:
-
FMDatabase:一個FMDatabase對象就代表一個單獨的 SQLite 數(shù)據(jù)庫,用來執(zhí)行 SQL 語句. -
FMResultSet使用FMDatabase執(zhí)行查詢后的結(jié)果集 -
FMDatabaseQueue:用于在多線程中執(zhí)行多個查詢或更新,它是線程安全的.
在使用FMDB 時,要使用終端進行導(dǎo)入.
- 創(chuàng)建 FMDatabase 對象時參數(shù)為 SQLite 數(shù)據(jù)庫文件路徑.該路徑可以是以下三種之一.
- 1,文件路徑,該文件路徑無需真實存在,如果路徑不存在會自動創(chuàng)建;
- 2,空字符串(@"").表示會在臨時目錄創(chuàng)建一個空的數(shù)據(jù)庫,當(dāng) FMDatabase 連接關(guān)閉時,文件也會被刪除.
- 3,NULL將創(chuàng)建一個內(nèi)在數(shù)據(jù)庫.同樣的.當(dāng) FMDatabase 連接關(guān)閉時,數(shù)據(jù)將會被銷毀.
@interface RootViewController ()
@property (nonatomic, strong) FMDatabase *database;//聲明 database 屬性.
@end
- (void)viewDidLoad {
[super viewDidLoad];
//建表操作
[self opertationNoResultWithSql:@"create table if not exists student(name text primary key ,sex text,age integer)" operationTag:@"建表"];
[self opertationNoResultWithSql:@"insert into student(name,sex,age) values ('張麗','女',23)" operationTag:@"插入數(shù)據(jù)"];
[self opertationNoResultWithSql:@"alter table student add number integer" operationTag:@"在插入一列表"];
NSArray *array = [self qureyWithSql:@"select * from student"];
NSLog(@"%@",array);
/**
* 1,有一個路徑來存儲數(shù)據(jù)庫文件
* 2,創(chuàng)建數(shù)據(jù)庫文件
* 3,增刪改查的操作
* 1)通過 FMDB ,能不能動態(tài)獲得一張表有多少個字段
* 2)FMDB 中,能不能動態(tài)獲得某一個字段的類型.
3)FMDB 中,能不能動態(tài)獲得字段的名稱.
*/
}
- 1,獲取路徑.
- (NSString *)dbPath{
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject)];
NSLog(@"%@",doc);
NSString *documents = [doc stringByAppendingPathComponent:@"fmdb.sqlite"];
return documents;
}
- 2,創(chuàng)建數(shù)據(jù)庫對象
- (FMDatabase *)openOrCreateDB{
/**
* 此寫法可以確保self.database = [FMDatabase databaseWithPath:[self dbPath]];
* 只會被執(zhí)行一次.不會在程序每次運行時,進行多次創(chuàng)建.
*/
static dispatch_once_t onceToken;
dispatch-once(&onceToken,^{
self.database = [FMDatabase databaseWithPath:[self dbPath];
});
//打開數(shù)據(jù)庫
if([self.database open]){
NSLog(@"打開成功");
return self.database;
}else{
NSLog(@"數(shù)據(jù)庫打開失敗");
return nil;
}
}
- 3,無返回結(jié)果集
-(BOOL)opertationNoResultWithSql:(NSString *)sql operationTag:(NSString *)tag{
//打開數(shù)據(jù)庫
FMDatabase *db = [self openOrCreateDB];
//執(zhí)行非查詢操作
BOOL isSuccess = [db executeUpdate:sql];
/**
* 當(dāng)操作完成,關(guān)閉數(shù)據(jù)庫
*/
[db close];
if (isSuccess) {
NSLog(@"%@操作成功",tag);
return YES;
}else{
NSLog(@"%@操作失敗",tag);
return NO;
}
}
- 4,查詢操作(又返回結(jié)果集)
- (NSArray*)qureyWithSql:(NSString *)sql{
//打開數(shù)據(jù)庫
FMDatabase *db = [self openOrCreateDB];
/**
* 執(zhí)行操作
* 1,執(zhí)行sql 語句,將返回結(jié)果先暫存到 resultSet 中
*/
FMResultSet *resultSet = [db executeQuery:sql];
//2,從 resultSet 中取出每一條記錄
/**
* next:做的是 sqlite3_step(stament) == row進行判斷;
*/
NSMutableArray *array = [[NSMutableArray alloc] init];
while ([resultSet next]) {
/**
* 當(dāng)字典建立在循環(huán)外面的時候.只是初始化一次.然而后面在進行查詢的額時候,會
* 將前面的結(jié)果給覆蓋掉.所以子啊每次執(zhí)行 while 循環(huán)的時候,都是
* 一個新的記錄被取出,所以我們需要一個新的字典來盛放新的記錄.所有我們進
* while 循環(huán)的時候都需要構(gòu)建一個新的字典對象.
*/
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSString *name = [resultSet stringForColumn:@"name"];
[dict setObject:name forKey:@"name"];
NSString *sex = [resultSet stringForColumn:@"sex"];
[dict setObject:sex forKey:@"sex"];
int age = [resultSet intForColumn:@"age"];
[dict setObject:@(age) forKey:@"age"];
int number = [resultSet intForColumn:@"number"];
[dict setObject:@(number) forKey:@"number"];
//將字典添加到數(shù)組中.
[array addObject:dict];
}
//釋放 resultSet
[resultSet close];
[db close];//關(guān)閉數(shù)據(jù)庫.
return array;
}