2011-10-07 148 views
0

我想用下面的代碼更新我的數據庫的一些值。但是這些值沒有更新,並且它保存爲null。數據庫更新問題

int NoOfItem = [str_NoOfitems integerValue]; 
int FurnitureId = [str_FurnitueId integerValue]; 
NSLog(@"FurnitureId %@", str_FurnitueId); 
@try { 
    if (sqlite3_open([[SQLDataFile getDBPath] UTF8String], &database) == SQLITE_OK) 
    { 

     if(updateStmt == nil) 
     { 
      NSString *sql = [NSString stringWithFormat:@"UPDATE FurnitureSelected Set NoOfItem = ? Where FurnitureId = %d" , FurnitureId]; 

      if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &updateStmt, NULL) != SQLITE_OK) 
      { 
       NSLog(@"erroe while creating Update statement %s",sqlite3_errmsg(database)); 

      }    
     } 

//  sqlite3_bind_text(updateStmt, 3, [str_NoOfitems UTF8String], -1, SQLITE_TRANSIENT); 

     sqlite3_bind_int(updateStmt, 3, NoOfItem); 
     if(SQLITE_DONE != sqlite3_step(updateStmt)) 
      NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database)); 



    } 

} 
@catch (NSException * e) { 
    NSLog(@"Exception occured while Updating datbase for FurnitureSelected %@", [e description]); 
} 
@finally 
{ 
    [SQLDataFile finalizeStatements]; 
} 

在上面的代碼中,沒有任何值爲空仍然數據庫在更新後存儲空值。在插入它完美的作品。

的插入碼是像(這被更新之前執行)

+(無效)InsertFurnitureValues:(的NSString *)str_FurnitureId:(的NSString *)str_NoOfItems:(的NSString *)str_RoomId {

int noOfItem = [str_NoOfItems integerValue]; 
int FId = [str_FurnitureId integerValue]; 
@try 
{ 

    if (sqlite3_open([[SQLDataFile getDBPath] UTF8String], &database) == SQLITE_OK) 
    { 

     if(addStmt == nil) 
     { 
      const char *sql = "insert into FurnitureSelected(AutoId,RoomId, FurnitureId, NoOfItem) Values(?,?, ?,?)"; 
      if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK) 
       NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database)); 
     } 

     sqlite3_bind_int(addStmt, 3, FId);//sqlite3_bind_text(addStmt, 2, [str_RoomId UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 2, [str_RoomId UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_int(addStmt, 4, noOfItem);//(addStmt, 4, [str_NoOfItems UTF8String], -1, SQLITE_TRANSIENT); 


     if(SQLITE_DONE != sqlite3_step(addStmt)) 
      NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database)); 


    sqlite3_reset(addStmt); 

    } 

} 
@catch (NSException * e) 
{ 
    NSLog(@"Exception occure %@" , [e description]); 
} 
@finally 
{ 
    [SQLDataFile finalizeStatements]; 
} 

}

請幫我解決這個問題。

回答

0

嘗試在準備好的語句上調用sqlite3_finalize。

+0

感謝您的回覆,但得到了解決方案 - 錯誤是 - sqlite3_bind_int(updateStmt,3,NoOfItem); ,如果我們只更新1個值,我們需要保留1而不是3。這是問題。 –