為什么使用第三方輕量級框架FMDB?
FMDB是用于進行數據存儲的第三方的框架,它與SQLite與Core Data相比較,存在很多優勢。
FMDB是面向對象的,它以OC的方式封裝了SQLite的C語言API,使用起來更加的方便,不需要過多的關心數據庫操作的知識。
為什么不使用core data和SQLite?
Core Data是ORM的一種體現,實現了界面化操作。使用Core Data需要用到模型數據的轉化,雖然操作簡單,不需要直接操作數據庫,但是性能沒有直接使用SQLite高。但是SQLite使用的時候需要使用c語言中的函數,操作麻煩,因此需要對它進行封裝。但是如果只是簡單地封裝,很可能會忽略很多重要的細節,比如如何處理并發以及安全性更問題。
下面簡單封裝sqlite來理解FMDB為什么那么好用呢?
新建SqleiteManage類實現封裝:
1 #import "SqleiteManage.h" 2 static SqleiteManage *manage = nil; 3 @implementation SqleiteManage 4 5 //單例保證是同一個數據庫 6 +(instancetype)shareManage{ 7 static dispatch_once_t onceToken; 8 dispatch_once(&onceToken, ^{ 9 manage = [[SqleiteManage alloc]init]; 10 11 }); 12 13 return manage; 14 } 15 16 //打開數據庫 17 -(int)openDB:(NSString *)str{ 18 //1.打開數據庫 建表 19 NSString *dbpath =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:str]; 20 NSLog(@"%@",dbpath); 21 22 //打開數據庫 23 result = sqlite3_open([dbpath UTF8String], &db); 24 if (SQLITE_OK ==result) { 25 NSLog(@"打開成功"); 26 }else{ 27 NSLog(@"打開失敗"); 28 } 29 30 return result; 31 32 } 33 34 //關閉數據庫 35 -(int)closeDB{ 36 return sqlite3_close(db); 37 } 38 //------建表 39 -(BOOL)creatTableWithSqlite:(NSString *)sql{ 40 41 if (result ==SQLITE_OK) { 42 43 // 建表的SQL語句 44 // PRimary key autoincrement 定義 id為主鍵 值是自動增長的 45 // not null unique 不能為空 不能重復 46 // 建表的公式 47 // create table 表名 (字段名 字段的數據類型,字段名 字段的數據類型........); 48 // NSString *sql =@"create table if not exists user (id integer primary key autoincrement, name text not null unique, phone text, creatDate text);"; 49 char *error; 50 int resul = sqlite3_exec(db, [sql UTF8String], NULL, NULL, &error); 51 [self closeDB]; 52 if (resul == SQLITE_OK) { 53 54 NSLog(@"建表成功"); 55 return YES; 56 // return 跳出整個函數 57 // black 是跳出括號 58 59 }else{ 60 NSLog(@"%s",error); 61 return NO; 62 } 63 64 } 65 return NO; 66 } 67 68 //插入 69 -(BOOL)insertMessageWithSql:(NSString *)sql{ 70 if (result == SQLITE_OK) { 71 char *error; 72 int resul = sqlite3_exec(db, [sql UTF8String], NULL, NULL, &error); 73 [self closeDB]; 74 if (resul == SQLITE_OK) { 75 return YES; 76 }else{ 77 return NO; 78 } 79 80 } 81 82 return NO; 83 84 } 85 86 //刪除 87 -(BOOL)deleteMessageWithSql:(NSString *)sql{ 88 if (result == SQLITE_OK) { 89 char *error; 90 int resul = sqlite3_exec(db, [sql UTF8String], NULL, NULL, &error); 91 [self closeDB]; 92 if (resul == SQLITE_OK) { 93 return YES; 94 }else{ 95 return NO; 96 } 97 98 } 99 return NO;100 }101 102 //修改103 -(BOOL)modifyMessageWithSql:(NSString *)sql{104 if (result == SQLITE_OK) {105 char *error;106 int resul = sqlite3_exec(db, [sql UTF8String], NULL, NULL, &error);107 [self closeDB];108 if (resul == SQLITE_OK) {109 return YES;110 }else{111 return NO;112 }113 114 }115 116 return NO;117 }118 119 -(NSArray*)queryMessageWithSQL:(NSString *)sql andObject:(NSString *)obj{120 if (result==SQLITE_OK) {121 // 聲明一個結果集 查詢的結果存放在結果集里面122 sqlite3_stmt *stmt;123 // 校驗SQL語句是否正確 int nByte 為-1的時候 不限制 查詢的長度124 if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, NULL)==SQLITE_OK) {125 // like 模糊查詢126 127 NSString *searchContent =[NSString stringWithFormat:@"%%%@%%",obj];128 // 綁定要查詢的內容129 if ( sqlite3_bind_text(stmt, 1, [searchContent UTF8String], -1, NULL)==SQLITE_OK130 ) {131 NSMutableArray * resultlist = [NSMutableArray array];132 // 循環 查詢133 while ( sqlite3_step(stmt)== SQLITE_ROW) {134 // 把查詢到的一條數據 整合到一個字典里面135 136 // 1 是 icol 查詢到的這一條數據的列數137 char *name =(char *) sqlite3_column_text(stmt, 1);138 char *phone = (char *) sqlite3_column_text(stmt, 2);139 char *time =(char *) sqlite3_column_text(stmt, 3);140 NSDictionary *info =@{@"name":[NSString stringWithUTF8String:name],@"phone":[NSString stringWithUTF8String:phone],@"time":[NSString stringWithUTF8String:time],};141 [resultlist addObject:info];142 }143 [self closeDB];144 return resultlist;145 146 }147 }148 149 }150 return nil;151 }152 @end
封裝后使用:
#import "ViewController.h"#import "SqleiteManage.h"@interface ViewController ()//數據庫(Database):按照數據結構來組織、存儲和管理數據//數據庫基本是由表,關系,操作構成//在移動平臺開發常用的是SQLite//以表(table)為單位//表頭的每一列 都是一個字段(clumn,屬性)//可以通過字段查找到對應的數據//ios 使用C語言操作數據庫//***** 使用數據庫之前的先添加;ibsqlite3框架#import<>@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad];//************數據庫相關概念*************** /* ios使用數據庫的重要方法 打開數據庫:sqlite3_open() 建表 修改 添加 更新 刪除數據:sqlite3_exec() 查詢:1.效驗語句是否合法:sqlite3_prepare_v2 2.綁定要查詢的數據和sql語句:sqlite3_bind_text 3.循環查找內容(根據行):sqlite3_step 4.取出這一行里面的數據(根據對應的類型):sqlite3_column_text 關閉數據庫:sqlite3_close() SQL(Strured Query Language)是一種結構查詢語言 SQL 語言特點:每一句后面都有一個;號結束 不區分大小寫 SQL 的關鍵字:create update delete from where by table 。。。 在數據庫里面不可以使用關鍵字來命名表名 或字段 數據庫中的字符串 要用單引號 '' 括起來 sqlite 是關系型數據庫 SQL語句使用公式 1.建表 @" create table (字段名 字段類型,字段名,字段類型);" 2.create table if not exists 表名(字段名,字段類型,) 如 :@"create table if not exists user(id integer,name text,phone text) 插入:insert into 表名(字段,字段)vlaus('內容','內容') 刪除:delete from 表名 where 字段= '要刪除的內容' 修改數據 :update 表名 set 字段 = '修改后的內容' where 字段 = '修改前的內容'; 查詢: (1)select *from 表名 查詢所有的字段(*表示所有); (2)sele 字段1,字段2,......from 表名; 數據庫的使用公式: 導入框架 1.創建數據庫 打開數據庫 2.創建表 3.添加 刪除 修改 查詢 內容 關閉數據庫 //*********************************************************** */ SqleiteManage * manage =[SqleiteManage shareManage];// 打開數據庫 [manage openDB:@"shujuku..sqlite"]; BOOL seccess = [manage creatTableWithSqlite: @"create table if not exists user (id integer primary key autoincrement, name text not null unique, phone text, creatDate text);"]; if (seccess) { NSLog(@"建表成功"); }// 插入數據公式// insert into 表名 (字段,字段,字段) values ('','',''); NSString *name = @"小啊"; NSString * tel = @"13298822122"; NSString * date = @"2088-12-25"; // 插入數據 NSString *sql = [NSString stringWithFormat:@"insert into user (name, phone,creatDate) values ('%@','%@','%@');", name,tel,date ]; if ([manage insertMessageWithSql:sql]==YES) { NSLog(@"插入數據成功"); } // 刪除數據 if ([manage deleteMessageWithSql:@"delete from user where name= '小明';"]==YES) { NSLog(@"刪除數據成功"); } if ([manage modifyMessageWithSql:@"update user set name = '大黃人' where name = '白馬王子';"]==YES) { NSLog(@"修改數據成功"); }// 查詢數據 NSArray * list = [manage queryMessageWithSQL:@"select id,name,phone,creatDate from user where name like ?;" andObject:@"小"]; if (list.count !=0) { NSLog(@"%@",list); }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
封裝后sqlite,減少了很多代碼,所有使用FMDB會非常好用。而且處理了多線程并發的問題。FMDB封裝也是這樣。
新聞熱點
疑難解答