
test.png
FMDB 增刪改查
FMDB 優(yōu)點(diǎn):
[1] 使用起來(lái)更加面向?qū)ο?,省去了很多麻煩、冗余的C語(yǔ)言代碼
[2] 對(duì)比蘋(píng)果自帶的Core Data框架,更加輕量級(jí)和靈活
[3] 提供了多線程安全的數(shù)據(jù)庫(kù)操作方法,有效地防止數(shù)據(jù)混亂核心類(lèi)
[1] FMDatabase:
一個(gè)FMDatabase對(duì)象就代表一個(gè)單獨(dú)的SQLite數(shù)據(jù)庫(kù)
用來(lái)執(zhí)行SQL語(yǔ)句
[2] FMResultSet:
使用FMDatabase執(zhí)行查詢(xún)后的結(jié)果集
[3] FMDatabaseQueue:
用于在多線程中執(zhí)行多個(gè)查詢(xún)或更新,它是線程安全的
#import "FMDBModel.h"
#import "FMDB.h"
// 創(chuàng)建數(shù)據(jù)庫(kù)
NSString *create = @"CREATE TABLE IF NOT EXISTS t_GESTURE (iPhone text NOT NULL ,password text NOT NULL)";
//delete from是刪除關(guān)鍵字,后面是要?jiǎng)h的表名,然后可以選擇跟一個(gè)where限制,限制參考修改
NSString *delete = @"delete from t_GESTURE where iPhone = ?";
//update是修改的關(guān)鍵字,后面是t_GESTURE表名,set表示修改動(dòng)作的關(guān)鍵字,set后面跟著被改的字段名及其要改的內(nèi)容(要改的內(nèi)容也可以是?),再后面可以加where限制,也可以不加,where后面是字段=內(nèi)容的結(jié)構(gòu),內(nèi)容同樣可以為?
NSString *updateTable = @"update t_GESTURE set password = ? where iPhone = ?";
// 查詢(xún)數(shù)據(jù)庫(kù)
NSString *selectTable = @"SELECT * FROM t_GESTURE";
@interface FMDBModel ()
@property (nonatomic,strong) FMDatabase *db;
@end
@implementation FMDBModel
+(FMDBModel *)fModel{
static FMDBModel *fModel = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (!fModel) {
fModel = [[FMDBModel alloc] init];
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"FMDB.sqlite"];
FMDatabase *db = [FMDatabase databaseWithPath:path];
[db open];
NSLog(@"%@",path);
[db executeUpdate:create];
[db close];
fModel.db = db;
}
});
return fModel;
}
- (void)insert{
[self.db open];
BOOL isInsert = [self.db executeUpdate:@"insert into t_GESTURE(iphone,password) VALUES (?,?)",self.phone,self.password];
if (isInsert) {
NSLog(@"插入成功");
}
[self.db close];
}
- (void)Delete{
[self.db open];
//2、調(diào)用刪除語(yǔ)句
BOOL isDelCharactor = [self.db executeUpdate:delete,self.phone];
if (isDelCharactor) {
NSLog(@"成功");
}
[self.db close];
}
- (void)change{
[self.db open];
//2、調(diào)用修改語(yǔ)句
BOOL isUpdate = [self.db executeUpdate:updateTable,self.password,self.phone];
if(isUpdate == NO)
{
NSLog(@"修改失敗");
return;
}else{
NSLog(@"修改成功");
}
[self.db close];
}
- (void)query{
[self.db open];
// 查詢(xún)
FMResultSet *resultSet = [self.db executeQuery:selectTable];
while ([resultSet next]) {
NSString *phone = [resultSet stringForColumn:@"iPhone"];
NSString *password = [resultSet stringForColumn:@"password"];
NSLog(@" %@ %@",phone,password);
}
[self.db close];
}
使用
// 插入
- (IBAction)ConfirmDown:(id)sender {
[FMDBModel fModel].phone = self.iPhone.text;
[FMDBModel fModel].password = self.password.text;
[[FMDBModel fModel] insert];
}
// 查詢(xún)
- (IBAction)SelectDown:(id)sender {
[[FMDBModel fModel] query];
}
// 刪除
- (IBAction)Delete:(id)sender {
[FMDBModel fModel].phone = self.iPhone.text;
[FMDBModel fModel].password = self.password.text;
[[FMDBModel fModel] Delete];
}
// 修改
- (IBAction)change:(id)sender {
[FMDBModel fModel].phone = self.iPhone.text;
[FMDBModel fModel].password = self.password.text;
[[FMDBModel fModel] change];
}