pragma mark ----定義----
- FMDBManager.h
把數(shù)據(jù)庫(kù)寫(xiě)成一個(gè)單例
#pragma mark ----單例----
+(FMDBManager *)shareInstance;
第一次運(yùn)行程序勘畔,會(huì)創(chuàng)建數(shù)據(jù)庫(kù) 并且鏈接數(shù)據(jù)庫(kù)地址
非第一次運(yùn)行程序 不會(huì)再創(chuàng)建數(shù)據(jù)庫(kù) 直接鏈接數(shù)據(jù)庫(kù)地址
#pragma mark ---打開(kāi)數(shù)據(jù)庫(kù)---
-(void)openDB;
在數(shù)據(jù)庫(kù)中創(chuàng)建表
#pragma mark ---在數(shù)據(jù)庫(kù)中創(chuàng)建表---
-(void)createTableWithTableName:(NSString *)tableName;
在數(shù)據(jù)庫(kù)中插入數(shù)據(jù)
#pragma mark ---添加數(shù)據(jù)---
-(void)insertDataWithTableName:(NSString *)tableName name:(NSString *)name gender:(NSString *)gender age:(NSInteger)age image:(UIImage *)image myID:(NSInteger)myID;
在特定表名中刪除數(shù)據(jù)
#pragma mark ---刪除數(shù)據(jù)---
-(void)deleteDataWithTableName:(NSString *)tableName myID:(NSInteger)myID;
在特定表名中修改數(shù)據(jù)
#pragma mark ---修改數(shù)據(jù)---
-(void)updataDataWithTableName:(NSString *)tableName name:(NSString *)name gender:(NSString *)gender age:(NSInteger)age image:(UIImage *)image myID:(NSInteger)myID forMyID:(NSInteger)forMyID;
查詢一條數(shù)據(jù)
#pragma mark ---查詢數(shù)據(jù)---
-(Model *)selectOneDataWithTableName:(NSString *)tableName myID:(NSInteger)myID;
查詢所有數(shù)據(jù)
#pragma mark ---查詢所有數(shù)據(jù)---
-(NSMutableArray *)selectAllDataWithTableName:(NSString *)tableName;
pragma mark ----實(shí)現(xiàn)----
2.FMDBManager.m
定義一個(gè)全局靜態(tài)變量
static FMDatabase *db = nil;
#pragma mark ----單例----
+(FMDBManager *)shareInstance
{
static FMDBManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[FMDBManager alloc]init];
});
return manager;
}
創(chuàng)建數(shù)據(jù)庫(kù),返回?cái)?shù)據(jù)庫(kù)路徑
-(NSString *)createSqliteWithSqliteName:(NSString *)sqliteName
{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:sqliteName];
}
第一次運(yùn)行程序,會(huì)創(chuàng)建數(shù)據(jù)庫(kù) 并且鏈接數(shù)據(jù)庫(kù)地址
非第一次運(yùn)行程序 不會(huì)再創(chuàng)建數(shù)據(jù)庫(kù) 直接鏈接數(shù)據(jù)庫(kù)地址
#pragma mark ---打開(kāi)數(shù)據(jù)庫(kù)---
-(void)openDB
{
if (db != nil) {
return;
}
NSString *path = [self createSqliteWithSqliteName:@"lol.sqlite"];
NSLog(@"path ===== %@",path);
db = [FMDatabase databaseWithPath:path];
> 打印信息带迟,確認(rèn)是否成功打開(kāi)數(shù)據(jù)庫(kù)
if ([db open]) {
NSLog(@"數(shù)據(jù)庫(kù)打開(kāi)成功");
}
else {
NSLog(@"數(shù)據(jù)庫(kù)打開(kāi)失敗");
}
}
為了查找錯(cuò)誤方便命锄,給一個(gè)flag值來(lái)觀察
#pragma mark ---在數(shù)據(jù)庫(kù)中創(chuàng)建表---
-(void)createTableWithTableName:(NSString *)tableName
{
[db open];
NSString *string = [NSString stringWithFormat:@"create table if not exists %@ (ID integer primary key autoincrement, name text, gender text, age integer, image blob, myID integer)",tableName];
BOOL flag = [db executeUpdate:string];
NSLog(@"創(chuàng)建表flag === %d",flag);
}
注意數(shù)據(jù)庫(kù)中數(shù)據(jù)的屬性,需要轉(zhuǎn)化成NSString或者NSNumber
>#pragma mark ---添加數(shù)據(jù)---
-(void)insertDataWithTableName:(NSString *)tableName name:(NSString *)name gender:(NSString *)gender age:(NSInteger)age image:(UIImage *)image myID:(NSInteger)myID
{
[db open];
NSString *string = [NSString stringWithFormat:@"insert into %@ (name, gender, age, image, myID) values (?, ?, ?, ?, ?)",tableName];
NSString *ageString = [NSString stringWithFormat:@"%ld",age];
NSString *myIDString = [NSString stringWithFormat:@"%ld",myID];
BOOL flag = [db executeUpdate:string,name,gender,ageString,UIImagePNGRepresentation(image),myIDString];
NSLog(@"插入數(shù)據(jù)flag ==== %d",flag);
}
注意數(shù)據(jù)庫(kù)中數(shù)據(jù)的屬性
#pragma mark ---在特定表名中刪除數(shù)據(jù)---
-(void)deleteDataWithTableName:(NSString *)tableName myID:(NSInteger)myID
{
[db open];
NSString *string = [NSString stringWithFormat:@"delete from %@ where myID = ?",tableName];
NSString *myIDString = [NSString stringWithFormat:@"%ld",myID];
BOOL flag = [db executeUpdate:string,myIDString];
NSLog(@"刪除數(shù)據(jù)flag === %d",flag);
}
>#pragma mark ---在特定表名中修改數(shù)據(jù)---
-(void)updataDataWithTableName:(NSString *)tableName name:(NSString *)name gender:(NSString *)gender age:(NSInteger)age image:(UIImage *)image myID:(NSInteger)myID forMyID:(NSInteger)forMyID
{
[db open];
NSString *string = [NSString stringWithFormat:@"update %@ set name = ?, gender = ?, age = ?, image = ?, myID = ? where myID = ?",tableName];
NSString *agestr = [NSString stringWithFormat:@"%ld",age];
NSString *myIDString = [NSString stringWithFormat:@"%ld",myID];
NSString *forMyIDString = [NSString stringWithFormat:@"%ld",forMyID];
BOOL flag = [db executeUpdate:string,name,gender,agestr,UIImagePNGRepresentation(image),myIDString,forMyIDString];
NSLog(@"更新數(shù)據(jù)flag ==== %d",flag);
}
注意數(shù)據(jù)屬性的轉(zhuǎn)化
>#pragma mark ---查詢數(shù)據(jù)---
-(Model *)selectOneDataWithTableName:(NSString *)tableName myID:(NSInteger)myID
{
Model *model = [[Model alloc]init];
[db open];
NSString *string = [NSString stringWithFormat:@"select * from %@ where myID = ?",tableName];
NSString *myIDString = [NSString stringWithFormat:@"%ld",myID];
FMResultSet *result = [db executeQuery:string,myIDString];
while ([result next]) {
model.name = [result stringForColumn:@"name"];
model.gender = [result stringForColumn:@"gender"];
model.age = [result intForColumn:@"age"];
NSData *data = [result dataForColumn:@"image"];
model.image = [UIImage imageWithData:data];
model.myID = [result intForColumn:@"myID"];
}
return model;
}
和上面類似万哪,就換了一下返回值
>#pragma mark ---查詢所有數(shù)據(jù)---
-(NSMutableArray *)selectAllDataWithTableName:(NSString *)tableName
{
NSMutableArray *modelArr = [NSMutableArray array];
[db open];
NSString *string = [NSString stringWithFormat:@"select * from %@",tableName];
FMResultSet *result = [db executeQuery:string];
while ([result next]) {
Model *model = [[Model alloc]init];
model.name = [result stringForColumn:@"name"];
model.gender = [result stringForColumn:@"gender"];
model.age = [result intForColumn:@"age"];
NSData *data = [result dataForColumn:@"image"];
model.image = [UIImage imageWithData:data];
model.myID = [result intForColumn:@"myID"];
[modelArr addObject:model];
}
return modelArr;
}