FMDB基于FMDatabaseQueue封装

创建数据库和数据表的工具类

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ff4647}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #de38a6}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ffffff}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #00b1ff}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #08fa95}span.s1 {font-variant-ligatures: no-common-ligatures; color: #eb905a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #ffffff}span.s4 {font-variant-ligatures: no-common-ligatures; color: #de38a6}span.s5 {font-variant-ligatures: no-common-ligatures; color: #08fa95}span.s6 {font-variant-ligatures: no-common-ligatures; color: #00b1ff}span.s7 {font-variant-ligatures: no-common-ligatures; color: #5231f4}span.s8 {font-variant-ligatures: no-common-ligatures; color: #ff4647}span.s9 {font-variant-ligatures: no-common-ligatures; color: #4bd157}span.s10 {font: 15.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #4bd157}span.s11 {font: 15.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #ff4647}

#import "GLSQLManger.h"
#import 
@implementation GLSQLManger
+(instancetype)shareManager{
    static GLSQLManger *instance;
    static dispatch_once_t Token;
    dispatch_once(&Token, ^{
        instance=[[self alloc]init];
        NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"status.db"];
        NSLog(@"%@",path);
        //创建数据库
        instance.queue=[FMDatabaseQueue databaseQueueWithPath:path];
        NSString *path1=[[NSBundle mainBundle]pathForResource:@"db.sql" ofType:nil];
        NSError *error;
        NSString *string=[NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:&error];
        //创表
        [instance.queue inDatabase:^(FMDatabase *db) {
            BOOL success=[db executeStatements:string];
            if (success) {
                NSLog(@"创表成功");
            }else{
                NSLog(@"创表失败");
            }
        }];
    });
    return instance;
}
@end

插入数据

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ff4647}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #08fa95}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #4bd157}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures; color: #ffffff}span.s2 {font-variant-ligatures: no-common-ligatures; color: #00b1ff}span.s3 {font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #de38a6}span.s5 {font-variant-ligatures: no-common-ligatures; color: #08fa95}span.s6 {font-variant-ligatures: no-common-ligatures; color: #5231f4}span.s7 {font-variant-ligatures: no-common-ligatures; color: #ff4647}span.s8 {font: 15.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #ff4647}

    NSString *string=@"INSERT INTO T_Person (name, age, height) VALUES (?, ?, ?);";
    [[GLSQLManger shareManager].queue inDatabase:^(FMDatabase *db) {
//       BOOL success=[db executeUpdate:string];
        BOOL success=[db executeUpdate:string values:@[@"zhang", @23, @1.9] error:nil];
        if (success) {
            NSLog(@"%lld",db.lastInsertRowId);
        }else{
            NSLog(@"插入失败");
        }
    }];

更新数据

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ff4647}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #08fa95}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ffffff}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #4bd157}span.s1 {font-variant-ligatures: no-common-ligatures; color: #ffffff}span.s2 {font-variant-ligatures: no-common-ligatures; color: #00b1ff}span.s3 {font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #de38a6}span.s5 {font-variant-ligatures: no-common-ligatures; color: #08fa95}span.s6 {font-variant-ligatures: no-common-ligatures; color: #5231f4}span.s7 {font-variant-ligatures: no-common-ligatures; color: #ff4647}span.s8 {font: 15.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #ff4647}

    NSString *string=@"UPDATE T_Person set name = ?, age = ?, height = ? WHERE id = ?;";
    [[GLSQLManger shareManager].queue inDatabase:^(FMDatabase *db) {
        BOOL result=[db executeUpdate:string values:@[@"lisi",@1,@1.5,@3] error:nil];
        if (result) {
            //NSLog(@"%lld",db.lastInsertRowId);
            NSLog(@"更新第3条数据成功");
        }else{
            NSLog(@"插入失败");
        }
    }];

删除 、更新 都需要指定查询条件即:WHERE id = ?

查询

1.简单查询
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ff4647}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #08fa95}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ffffff}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ffffff; min-height: 17.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #ffffff}span.s2 {font-variant-ligatures: no-common-ligatures; color: #00b1ff}span.s3 {font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #08fa95}span.s5 {font-variant-ligatures: no-common-ligatures; color: #de38a6}span.s6 {font-variant-ligatures: no-common-ligatures; color: #ff4647}

    NSString *string=@"SELECT id, name, age, height FROM T_Person;";
    [[GLSQLManger shareManager].queue inDatabase:^(FMDatabase *db) {
        FMResultSet *set=[db executeQuery:string];
        while ([set next]) {
            NSString *dd=set[@"id"];
            NSString *name=set[@"name"];
            NSLog(@"%@---%@",dd,name);
        }
        
        
    }];
2.简单查询
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ff4647}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #08fa95}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ffffff}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #00b1ff}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ffffff; min-height: 17.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #ffffff}span.s2 {font-variant-ligatures: no-common-ligatures; color: #00b1ff}span.s3 {font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #08fa95}span.s5 {font-variant-ligatures: no-common-ligatures; color: #5231f4}span.s6 {font-variant-ligatures: no-common-ligatures; color: #de38a6}span.s7 {font-variant-ligatures: no-common-ligatures; color: #ff4647}

    NSString *string=@"SELECT id, name, age, height FROM T_Person;";
    [[GLSQLManger shareManager].queue inDatabase:^(FMDatabase *db) {
        FMResultSet *set=[db executeQuery:string];
        NSMutableArray *arrayMu=[NSMutableArray arrayWithCapacity:4];
        while ([set next]) {
            NSMutableDictionary *row=[NSMutableDictionary dictionaryWithCapacity:4];
            row[@"id"]=set[@"id"];
            row[@"name"]=set[@"name"];
            row[@"age"]=set[@"age"];
            row[@"height"]=set[@"height"];
            [arrayMu addObject:row];
         }
        NSLog(@"%@",arrayMu);
        
    }];
3.常用查询
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #ffffff}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #08fa95}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px 'Andale Mono'; color: #00b1ff}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #de38a6}span.s3 {font-variant-ligatures: no-common-ligatures; color: #00b1ff}span.s4 {font-variant-ligatures: no-common-ligatures; color: #ffffff}span.s5 {font-variant-ligatures: no-common-ligatures; color: #08fa95}span.s6 {font-variant-ligatures: no-common-ligatures; color: #5231f4}span.s7 {font-variant-ligatures: no-common-ligatures; color: #4bd157}span.s8 {font: 15.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #4bd157}span.s9 {font-variant-ligatures: no-common-ligatures; color: #ff4647}

-(void)demo5:(NSString *)string {
    [[GLSQLManger shareManager].queue inDatabase:^(FMDatabase *db) {
        FMResultSet *set=[db executeQuery:string];
        NSMutableArray *arrayMu=[NSMutableArray arrayWithCapacity:4];
        while ([set next]) {
            NSMutableDictionary *row=[NSMutableDictionary dictionaryWithCapacity:4];
            //列数
            int colCount=[set columnCount];
            for (int i=0; i

你可能感兴趣的:(FMDB基于FMDatabaseQueue封装)