qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問 http://qaseven.github.io/

          IOS的一個(gè)數(shù)據(jù)庫方法

           我在IOS編程使用的FMDatabase這個(gè)sqlite框架,需要客戶端與服務(wù)器做一些數(shù)據(jù)同步工作,有時(shí)得執(zhí)行比較多的命令(增刪改表當(dāng)然是手寫,但如果要手寫插入幾百上千條記錄是很不現(xiàn)實(shí)的,也只需要用php從mysql中讀取記錄便可),而且因?yàn)榭蛻舳说木壒剩琺ysql數(shù)據(jù)庫里的一些字段并不需要寫入到客戶端的sqlite中,所以我們可以用PHP寫一個(gè)接口頁面,以JSON傳遞我們希望傳達(dá)的數(shù)據(jù),然后再在客戶端進(jìn)行處理。

            傳遞的數(shù)據(jù)有兩種格式,一種是直接執(zhí)行的命令,我把它存放在“query”數(shù)組當(dāng)中,另一種是要插入的記錄,把它存放在“record”當(dāng)中。“query”中的的命令直接執(zhí)行,而“record”里的記錄以“key”=>“value”的方式,在客戶端循環(huán)出SQL語句執(zhí)行。

          $update_array['database']['query'] = array();

          $update_array['table']['wares_category']['query'] = array();
          $update_array['table']['wares_category']['record'] = $all_wares_category;

          //客戶端的同步函數(shù)

          -(void)databaseUpdate
          {
             FMDatabase *db = [self getDatabase];
            
             NSURL *updateUrl = [[NSURL alloc]initWithString:[Api stringByAppendingPathComponent:@"databaseUpdate/databaseupdate"]];
             NSData *updateData = [[NSData alloc]initWithContentsOfURL:updateUrl];
            
            
             NSError *error = nil;
            
             NSDictionary *updateDictionary = [NSJSONSerialization JSONObjectWithData:updateData options:NSJSONReadingMutableContainers error:&error];
            
             int i;
             //database數(shù)組
             if([[[updateDictionary objectForKey:@"database"]objectForKey:@"query"] count] > 0){
                 for (i = 0; i < [[[updateDictionary objectForKey:@"database"]objectForKey:@"query"] count]; i++){
                     NSString *query = [[[updateDictionary objectForKey:@"database"]objectForKey:@"query"]objectAtIndex:i];
                     [db executeUpdate:query];
                 }
             }
            
             //table數(shù)組
             if([[updateDictionary objectForKey:@"table"] count] > 0){
                
                 for(id tableName in [updateDictionary objectForKey:@"table"]){
                    
                     if([[[[updateDictionary objectForKey:@"table"] objectForKey:tableName] objectForKey:@"query"] count] > 0){
                         for (i = 0; i < [[[[updateDictionary objectForKey:@"table"] objectForKey:tableName] objectForKey:@"query"] count]; i++){
                             NSString *query = [[[[updateDictionary objectForKey:@"table"] objectForKey:tableName] objectForKey:@"query"]objectAtIndex:i];
                             [db executeUpdate:query];
                         }
                     }
                    
                     if([[[[updateDictionary objectForKey:@"table"] objectForKey:tableName] objectForKey:@"record"] count] > 0){
                         for (i = 0; i < [[[[updateDictionary objectForKey:@"table"] objectForKey:tableName] objectForKey:@"record"] count]; i++){
                            
                             NSMutableArray *keys = [[NSMutableArray alloc] init];
                             NSMutableArray *values = [[NSMutableArray alloc] init];
                            
                             for (id fieldsName in [[[[updateDictionary objectForKey:@"table"] objectForKey:tableName] objectForKey:@"record"]objectAtIndex:i]){
                                 NSString *fieldsValue = [[[[[updateDictionary objectForKey:@"table"] objectForKey:tableName] objectForKey:@"record"]objectAtIndex:i]objectForKey:fieldsName];
                                 [keys addObject:[NSString stringWithFormat:@"'%@'",fieldsName]];
                                
                                
                                 if(fieldsValue == (NSString*)[NSNull null]){
                                     fieldsValue = @"";
                                 }
                                 [values addObject:[NSString stringWithFormat:@"'%@'",fieldsValue]];
                             }
                            
                             NSString *keyString = [keys componentsJoinedByString:@", "];
                            
                             NSString *valueString = [values componentsJoinedByString:@", "];
                             NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (%@)",tableName, keyString, valueString];
                             [self alertByString:sql];
                             [db executeUpdate:sql];
                         }
                     }
                 }
             }
          }

          posted on 2013-09-04 10:41 順其自然EVO 閱讀(232) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2013年9月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 同心县| 神农架林区| 万安县| 久治县| 平江县| 哈尔滨市| 宕昌县| 石阡县| 丹阳市| 北海市| 榆林市| 肥西县| 茶陵县| 美姑县| 个旧市| 焉耆| 木里| 当阳市| 什邡市| 揭东县| 漯河市| 柳河县| 穆棱市| 平定县| 宕昌县| 云南省| 金乡县| 胶南市| 霍城县| 济阳县| 泊头市| 平舆县| 漠河县| 日喀则市| 乐平市| 东源县| 曲阳县| 连城县| 南华县| 图片| 磴口县|