2013-03-11 127 views
0

嗨,我已經在我的應用程序委託刪除冗餘的數據庫中的功能,我不知道我有編碼的權利,因爲我必須執行嵌套的SQL語句來找出數據庫中的錯誤。嵌套的Sql語句

任何機構都可以告訴我我錯了,因爲應用程序在模擬器中運行良好,並在設備中崩潰。

我什至不知道把finalize和sqlite3_close放在哪裏。好心幫

-(void) removeRedundancy2 
    { 
     NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     dbPathString = [[docPaths objectAtIndex:0] stringByAppendingPathComponent:@"turfnutritiontool_ver_99.db"]; 
     sqlite3_stmt *selectStmt; 
     sqlite3_stmt *selectStmt1; 
     BOOL isMyFileThere = [[NSFileManager defaultManager] fileExistsAtPath:dbPathString]; 

     if (isMyFileThere) 
     { 
      if (sqlite3_open([dbPathString UTF8String], &database1)==SQLITE_OK) 
      { 
       // TO REMOVE FROM from tnt_scenario_product when NO ProductID Found 

       NSString *querySql2= [NSString stringWithFormat:@"SELECT productid from tnt_scenarioproduct"]; 
       const char* query_sql2 = [querySql2 UTF8String]; 

       if(sqlite3_prepare_v2(database1, query_sql2, -1, &selectStmt, NULL) == SQLITE_OK) 
      { 
       while (sqlite3_step(selectStmt) == SQLITE_ROW) 
       { 
        int productid = sqlite3_column_int(selectStmt, 0); 
        // NSLog(@"ProductId1 =%d",productid); 

        NSString *querySql21= [NSString stringWithFormat:@"SELECT productid from tnt_productcontent WHERE productid = %d",productid]; 
        const char* query_sql21 = [querySql21 UTF8String]; 

        if(sqlite3_prepare_v2(database1, query_sql21, -1, &selectStmt1, NULL) == SQLITE_OK) 
        { 
         if (sqlite3_step(selectStmt1) == SQLITE_ROW) 
         { 
          // DO NOTHING 
         } 
         else 
         { // to delete scenario without product id 
          NSLog(@"Delete this Product from TPC 2 %d",productid); 
          NSString *querydelete2= [NSString stringWithFormat:@"DELETE from tnt_scenarioproduct WHERE productid = %d",productid]; 

          const char* query_delete2 = [querydelete2 UTF8String]; 
          char *error; 
          sqlite3_exec(database1, query_delete2, NULL, NULL, &error); 
          NSLog(@"error=%s ",error); 
          sqlite3_finalize(selectStmt1); 
         } 
        } 
        sqlite3_finalize(selectStmt1); 
        sqlite3_close(database1); 

       } 
       sqlite3_finalize(selectStmt); 
      } 
      sqlite3_close(database1); 
     } 
     sqlite3_close(database1); 

    } 
} 
+0

您是否閱讀過[sqlite3_open](http://www.sqlite.org/c3ref/open.html)/ [close](http://www.sqlite.org/c3ref/close.html)的文檔/[prepare](http://www.sqlite.org/c3ref/prepare.html)/[finalize](http://www.sqlite.org/c3ref/finalize.html)? – 2013-03-11 16:03:21

+0

我已經使用嵌套的語句,所以我無法捕捉到哪裏完成並關閉:( – 2013-03-12 06:10:29

+0

所以你不明白如何(目標)C的作品? – 2013-03-12 08:39:14

回答

0

調用sqlite3_open後,你必須電話sqlite3_close該連接一次。 致電sqlite3_prepare_v2後,您的必須對於該語句只需撥打sqlite3_finalize一次。

if (sqlite3_open([dbPathString UTF8String], &database1)==SQLITE_OK) 
{ 
    ... 
    if(sqlite3_prepare_v2(database1, query_sql21, -1, &selectStmt1, NULL) == SQLITE_OK) 
    { 
     ... 
    } 
    sqlite3_finalize(selectStmt); 
    .... 
} 
sqlite3_close(database1); 

此外,你不應該重複使用相同的變量(selectStmt)兩個不同的查詢,以防止有關其生命週期的混亂。