学习笔记---FMDB数据库的封装

pragma mark ----定义----

  1. FMDBManager.h

把数据库写成一个单例

#pragma mark ----单例----
+(FMDBManager *)shareInstance;

第一次运行程序,会创建数据库 并且链接数据库地址
非第一次运行程序 不会再创建数据库 直接链接数据库地址

#pragma mark ---打开数据库---
-(void)openDB;

在数据库中创建表

#pragma mark ---在数据库中创建表---
-(void)createTableWithTableName:(NSString *)tableName;

在数据库中插入数据

#pragma mark ---添加数据---
-(void)insertDataWithTableName:(NSString *)tableName name:(NSString *)name gender:(NSString *)gender age:(NSInteger)age image:(UIImage *)image myID:(NSInteger)myID;

在特定表名中删除数据

#pragma mark ---删除数据---
-(void)deleteDataWithTableName:(NSString *)tableName  myID:(NSInteger)myID;

在特定表名中修改数据

#pragma mark ---修改数据---
-(void)updataDataWithTableName:(NSString *)tableName name:(NSString *)name gender:(NSString *)gender age:(NSInteger)age image:(UIImage *)image myID:(NSInteger)myID forMyID:(NSInteger)forMyID;

查询一条数据

#pragma mark ---查询数据---
-(Model *)selectOneDataWithTableName:(NSString *)tableName myID:(NSInteger)myID;

查询所有数据

#pragma mark ---查询所有数据---
-(NSMutableArray *)selectAllDataWithTableName:(NSString *)tableName;

pragma mark ----实现----

2.FMDBManager.m

定义一个全局静态变量

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;
}

创建数据库,返回数据库路径

-(NSString *)createSqliteWithSqliteName:(NSString *)sqliteName
{
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:sqliteName];
}

第一次运行程序,会创建数据库 并且链接数据库地址
非第一次运行程序 不会再创建数据库 直接链接数据库地址

#pragma mark ---打开数据库---
-(void)openDB
{
    if (db != nil) {
        return;
    }
    NSString *path = [self createSqliteWithSqliteName:@"lol.sqlite"];
    NSLog(@"path ===== %@",path);
    db = [FMDatabase databaseWithPath:path];
> 打印信息,确认是否成功打开数据库
    if ([db open]) {
        NSLog(@"数据库打开成功");
    }
    else {
        NSLog(@"数据库打开失败");
    }    
}

为了查找错误方便,给一个flag值来观察

#pragma mark ---在数据库中创建表---
-(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(@"创建表flag === %d",flag);
}

注意数据库中数据的属性,需要转化成NSString或者NSNumber

>#pragma mark ---添加数据---
-(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(@"插入数据flag ==== %d",flag);    
}

注意数据库中数据的属性

#pragma mark ---在特定表名中删除数据---
-(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(@"删除数据flag === %d",flag);    
}
>#pragma mark ---在特定表名中修改数据---
-(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(@"更新数据flag ==== %d",flag);    
}

注意数据属性的转化

>#pragma mark ---查询数据---
-(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 ---查询所有数据---
-(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;
}

你可能感兴趣的:(学习笔记---FMDB数据库的封装)