最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS學(xué)習(xí)筆記(十六)——詳解數(shù)據(jù)庫操作(使用FMDB)

 更新時(shí)間:2016年12月13日 09:23:16   作者:xyz_lmn  
這篇文章主要介紹了iOS學(xué)習(xí)筆記(十六)——詳解數(shù)據(jù)庫操作(使用FMDB),具有一定的參考價(jià)值,有興趣的可以了解一下。

iOS中原生的SQLite API在使用上相當(dāng)不友好,在使用時(shí),非常不便。于是,就出現(xiàn)了一系列將SQLite API進(jìn)行封裝的庫,例如FMDB、PlausibleDatabase、sqlitepersistentobjects等,F(xiàn)MDB (https://github.com/ccgus/fmdb) 是一款簡潔、易用的封裝庫,這一篇文章簡單介紹下FMDB的使用。

在FMDB下載文件后,工程中必須導(dǎo)入如下文件,并使用 libsqlite3.dylib 依賴包。

FMDB同時(shí)兼容ARC和非ARC工程,會(huì)自動(dòng)根據(jù)工程配置來調(diào)整相關(guān)的內(nèi)存管理代碼。

FMDB常用類:

  1. FMDatabase : 一個(gè)單一的SQLite數(shù)據(jù)庫,用于執(zhí)行SQL語句。
  2. FMResultSet :執(zhí)行查詢一個(gè)FMDatabase結(jié)果集,這個(gè)和Android的Cursor類似。
  3. FMDatabaseQueue :在多個(gè)線程來執(zhí)行查詢和更新時(shí)會(huì)使用這個(gè)類。

創(chuàng)建數(shù)據(jù)庫:

db = [FMDatabase databaseWithPath:database_path]; 

1、當(dāng)數(shù)據(jù)庫文件不存在時(shí),fmdb會(huì)自己創(chuàng)建一個(gè)。

2、 如果你傳入的參數(shù)是空串:@"" ,則fmdb會(huì)在臨時(shí)文件目錄下創(chuàng)建這個(gè)數(shù)據(jù)庫,數(shù)據(jù)庫斷開連接時(shí),數(shù)據(jù)庫文件被刪除。

3、如果你傳入的參數(shù)是 NULL,則它會(huì)建立一個(gè)在內(nèi)存中的數(shù)據(jù)庫,數(shù)據(jù)庫斷開連接時(shí),數(shù)據(jù)庫文件被刪除。

打開數(shù)據(jù)庫:

[db open] 

返回BOOL型。

關(guān)閉數(shù)據(jù)庫:

[db close] 

數(shù)據(jù)庫增刪改等操作:

除了查詢操作,F(xiàn)MDB數(shù)據(jù)庫操作都執(zhí)行executeUpdate方法,這個(gè)方法返回BOOL型。

看一下例子:

創(chuàng)建表:

if ([db open]) { 
    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]; 
 
  } 

添加數(shù)據(jù):

if ([db open]) { 
    NSString *insertSql1= [NSString stringWithFormat: 
               @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')", 
               TABLENAME, NAME, AGE, ADDRESS, @"張三", @"13", @"濟(jì)南"]; 
    BOOL res = [db executeUpdate:insertSql1]; 
    NSString *insertSql2 = [NSString stringWithFormat: 
                @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')", 
                TABLENAME, NAME, AGE, ADDRESS, @"李四", @"12", @"濟(jì)南"]; 
    BOOL res2 = [db executeUpdate:insertSql2]; 
     
    if (!res) { 
      NSLog(@"error when insert db table"); 
    } else { 
      NSLog(@"success to insert db table"); 
    } 
    [db close]; 
 
  } 

修改數(shù)據(jù):

if ([db open]) { 
    NSString *updateSql = [NSString stringWithFormat: 
                @"UPDATE '%@' SET '%@' = '%@' WHERE '%@' = '%@'", 
                TABLENAME,  AGE, @"15" ,AGE, @"13"]; 
    BOOL res = [db executeUpdate:updateSql]; 
    if (!res) { 
      NSLog(@"error when update db table"); 
    } else { 
      NSLog(@"success to update db table"); 
    } 
    [db close]; 
 
  } 

刪除數(shù)據(jù):

if ([db open]) { 
     
    NSString *deleteSql = [NSString stringWithFormat: 
                @"delete from %@ where %@ = '%@'", 
                TABLENAME, NAME, @"張三"]; 
    BOOL res = [db executeUpdate:deleteSql]; 
     
    if (!res) { 
      NSLog(@"error when delete db table"); 
    } else { 
      NSLog(@"success to delete db table"); 
    } 
    [db close]; 
 
  } 

數(shù)據(jù)庫查詢操作:

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

if ([db open]) { 
    NSString * sql = [NSString stringWithFormat: 
             @"SELECT * FROM %@",TABLENAME]; 
    FMResultSet * rs = [db executeQuery:sql]; 
    while ([rs next]) { 
      int Id = [rs intForColumn:ID]; 
      NSString * name = [rs stringForColumn:NAME]; 
      NSString * age = [rs stringForColumn:AGE]; 
      NSString * address = [rs stringForColumn:ADDRESS]; 
      NSLog(@"id = %d, name = %@, age = %@ address = %@", Id, name, age, address); 
    } 
    [db close]; 
  } 

FMDB的FMResultSet提供了多個(gè)方法來獲取不同類型的數(shù)據(jù):

 

數(shù)據(jù)庫多線程操作:

如果應(yīng)用中使用了多線程操作數(shù)據(jù)庫,那么就需要使用FMDatabaseQueue來保證線程安全了。 應(yīng)用中不可在多個(gè)線程中共同使用一個(gè)FMDatabase對(duì)象操作數(shù)據(jù)庫,這樣會(huì)引起數(shù)據(jù)庫數(shù)據(jù)混亂。 為了多線程操作數(shù)據(jù)庫安全,F(xiàn)MDB使用了FMDatabaseQueue,使用FMDatabaseQueue很簡單,首先用一個(gè)數(shù)據(jù)庫文件地址來初使化FMDatabaseQueue,然后就可以將一個(gè)閉包(block)傳入inDatabase方法中。 在閉包中操作數(shù)據(jù)庫,而不直接參與FMDatabase的管理。

FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath:database_path]; 
  dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL); 
  dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL); 
   
  dispatch_async(q1, ^{ 
    for (int i = 0; i < 50; ++i) { 
      [queue inDatabase:^(FMDatabase *db2) { 
         
        NSString *insertSql1= [NSString stringWithFormat: 
                   @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES (?, ?, ?)", 
                   TABLENAME, NAME, AGE, ADDRESS]; 
         
        NSString * name = [NSString stringWithFormat:@"jack %d", i]; 
        NSString * age = [NSString stringWithFormat:@"%d", 10+i]; 
         
         
        BOOL res = [db2 executeUpdate:insertSql1, name, age,@"濟(jì)南"]; 
        if (!res) { 
          NSLog(@"error to inster data: %@", name); 
        } else { 
          NSLog(@"succ to inster data: %@", name); 
        } 
      }]; 
    } 
  }); 
   
  dispatch_async(q2, ^{ 
    for (int i = 0; i < 50; ++i) { 
      [queue inDatabase:^(FMDatabase *db2) { 
        NSString *insertSql2= [NSString stringWithFormat: 
                   @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES (?, ?, ?)", 
                   TABLENAME, NAME, AGE, ADDRESS]; 
         
        NSString * name = [NSString stringWithFormat:@"lilei %d", i]; 
        NSString * age = [NSString stringWithFormat:@"%d", 10+i]; 
         
        BOOL res = [db2 executeUpdate:insertSql2, name, age,@"北京"]; 
        if (!res) { 
          NSLog(@"error to inster data: %@", name); 
        } else { 
          NSLog(@"succ to inster data: %@", name); 
        } 
      }]; 
    } 
  }); 

源碼下載:DEMO

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

册亨县| 黎城县| 德昌县| 新竹县| 方山县| 泾源县| 安平县| 西乌| 伊宁县| 定边县| 教育| 道孚县| 湘潭县| 双辽市| 开远市| 贵港市| 方正县| 遵化市| 十堰市| 准格尔旗| 泾源县| 徐州市| 德钦县| 叙永县| 济南市| 赣榆县| 长顺县| 平度市| 衡南县| 眉山市| 定西市| 高碑店市| 广安市| 甘德县| 库尔勒市| 阿尔山市| 昌乐县| 合水县| 华池县| 河东区| 河西区|