当前位置:Gxlcms > 数据库问题 > FMDB

FMDB

时间:2021-07-01 10:21:17 帮助过:7人阅读

  •         NSString *sqlCreateTable =  [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS ‘%@‘ (‘%@‘ INTEGER PRIMARY KEY AUTOINCREMENT, ‘%@‘ TEXT, ‘%@‘ INTEGER, ‘%@‘ TEXT)",TABLENAME,ID,NAME,AGE,ADDRESS];  
  •         BOOL res = [db executeUpdate:sqlCreateTable];  
  •         if (!res) {  
  •             NSLog(@"error when creating db table");  
  •         } else {  
  •             NSLog(@"success to creating db table");  
  •         }  
  •         [db close];  
  •   
  •     }  


  •  

    添加数据:

     

    1. if ([db open]) {  
    2.        NSString *insertSql1= [NSString stringWithFormat:  
    3.                               @"INSERT INTO ‘%@‘ (‘%@‘, ‘%@‘, ‘%@‘) VALUES (‘%@‘, ‘%@‘, ‘%@‘)",  
    4.                               TABLENAME, NAME, AGE, ADDRESS, @"张三", @"13", @"济南"];  
    5.        BOOL res = [db executeUpdate:insertSql1];  
    6.        NSString *insertSql2 = [NSString stringWithFormat:  
    7.                                @"INSERT INTO ‘%@‘ (‘%@‘, ‘%@‘, ‘%@‘) VALUES (‘%@‘, ‘%@‘, ‘%@‘)",  
    8.                                TABLENAME, NAME, AGE, ADDRESS, @"李四", @"12", @"济南"];  
    9.        BOOL res2 = [db executeUpdate:insertSql2];  
    10.          
    11.        if (!res) {  
    12.            NSLog(@"error when insert db table");  
    13.        } else {  
    14.            NSLog(@"success to insert db table");  
    15.        }  
    16.        [db close];  
    17.   
    18.    }  



     

    修改数据:

     

    [cpp] view plaincopyprint?
    1. if ([db open]) {  
    2.         NSString *updateSql = [NSString stringWithFormat:  
    3.                                @"UPDATE ‘%@‘ SET ‘%@‘ = ‘%@‘ WHERE ‘%@‘ = ‘%@‘",  
    4.                                TABLENAME,   AGE,  @"15" ,AGE,  @"13"];  
    5.         BOOL res = [db executeUpdate:updateSql];  
    6.         if (!res) {  
    7.             NSLog(@"error when update db table");  
    8.         } else {  
    9.             NSLog(@"success to update db table");  
    10.         }  
    11.         [db close];  
    12.   
    13.     }  



     

    删除数据:

     

    1. if ([db open]) {  
    2.           
    3.         NSString *deleteSql = [NSString stringWithFormat:  
    4.                                @"delete from %@ where %@ = ‘%@‘",  
    5.                                TABLENAME, NAME, @"张三"];  
    6.         BOOL res = [db executeUpdate:deleteSql];  
    7.           
    8.         if (!res) {  
    9.             NSLog(@"error when delete db table");  
    10.         } else {  
    11.             NSLog(@"success to delete db table");  
    12.         }  
    13.         [db close];  
    14.   
    15.     }  



     

    数据库查询操作:

    查询操作使用了executeQuery,并涉及到FMResultSet。

     

    1. if ([db open]) {  
    2.         NSString * sql = [NSString stringWithFormat:  
    3.                           @"SELECT * FROM %@",TABLENAME];  
    4.         FMResultSet * rs = [db executeQuery:sql];  
    5.         while ([rs next]) {  
    6.             int Id = [rs intForColumn:ID];  
    7.             NSString * name = [rs stringForColumn:NAME];  
    8.             NSString * age = [rs stringForColumn:AGE];  
    9.             NSString * address = [rs stringForColumn:ADDRESS];  
    10.             NSLog(@"id = %d, name = %@, age = %@  address = %@", Id, name, age, address);  
    11.         }  
    12.         [db close];  
    13.     }  

     

     

    FMDB的FMResultSet提供了多个方法来获取不同类型的数据:

    技术分享

    数据库多线程操作:

     

            如果应用中使用了多线程操作数据库,那么就需要使用FMDatabaseQueue来保证线程安全了。 应用中不可在多个线程中共同使用一个FMDatabase对象操作数据库,这样会引起数据库数据混乱。 为了多线程操作数据库安全,FMDB使用了FMDatabaseQueue,使用FMDatabaseQueue很简单,首先用一个数据库文件地址来初使化FMDatabaseQueue,然后就可以将一个闭包(block)传入inDatabase方法中。 在闭包中操作数据库,而不直接参与FMDatabase的管理。

    [cpp] view plaincopyprint?
      1. FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath:database_path];  
      2.    dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);  
      3.    dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL);  
      4.      
      5.    dispatch_async(q1, ^{  
      6.        for (int i = 0; i < 50; ++i) {  
      7.            [queue inDatabase:^(FMDatabase *db2) {  
      8.                  
      9.                NSString *insertSql1= [NSString stringWithFormat:  
      10.                                       @"INSERT INTO ‘%@‘ (‘%@‘, ‘%@‘, ‘%@‘) VALUES (?, ?, ?)",  
      11.                                       TABLENAME, NAME, AGE, ADDRESS];  
      12.                  
      13.                NSString * name = [NSString stringWithFormat:@"jack %d", i];  
      14.                NSString * age = [NSString stringWithFormat:@"%d", 10+i];  
      15.                  
      16.                  
      17.                BOOL res = [db2 executeUpdate:insertSql1, name, age,@"济南"];  
      18.                if (!res) {  
      19.                    NSLog(@"error to inster data: %@", name);  
      20.                } else {  
      21.                    NSLog(@"succ to inster data: %@", name);  
      22.                }  
      23.            }];  
      24.        }  
      25.    });  
      26.      
      27.    dispatch_async(q2, ^{  
      28.        for (int i = 0; i < 50; ++i) {  
      29.            [queue inDatabase:^(FMDatabase *db2) {  
      30.                NSString *insertSql2= [NSString stringWithFormat:  
      31.                                       @"INSERT INTO ‘%@‘ (‘%@‘, ‘%@‘, ‘%@‘) VALUES (?, ?, ?)",  
      32.                                       TABLENAME, NAME, AGE, ADDRESS];  
      33.                  
      34.                NSString * name = [NSString stringWithFormat:@"lilei %d", i];  
      35.                NSString * age = [NSString stringWithFormat:@"%d", 10+i];  
      36.                  
      37.                BOOL res = [db2 executeUpdate:insertSql2, name, age,@"北京"];  
      38.                if (!res) {  
      39.                    NSLog(@"error to inster data: %@", name);  
      40.                } else {  
      41.                    NSLog(@"succ to inster data: %@", name);  
      42.                }  
      43.            }];  
      44.        }  
      45.    });  

    FMDB

    标签:

    人气教程排行