2013-11-04 83 views
0

我已經做了很多關於更新創建的sqlite表的搜索結果,但我仍然無法在示例應用程序中更新我的表。可以請任何人告訴我下面的代碼有什麼問題。它工作正常,直到sqlite3_prepare_v2。一旦它達到如果(sqlite3_prepare_v2(數據庫,SQL,-1,&語句,NULL)== SQLITE_OK)條件它不會進入此if()條件是否可以告訴什麼是在這裏發生?無法更新SQlite3表

const char *dbpath = [[self DBPath] UTF8String]; 
if(sqlite3_open(dbpath, &database) == SQLITE_OK) 
{ 

    NSString *querySql=[NSString stringWithFormat:@"UPDATE Table1 SET AppEndTime = %@ WHERE AppID= %d",AppEndTime,appID]; 
    const char *sql=[querySql UTF8String]; 
    if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL)==SQLITE_OK){ 
     sqlite3_bind_int(statement, 1, sessionID); 
     sqlite3_bind_text(statement, 6, [sessionEndTime UTF8String], -1, SQLITE_TRANSIENT); 

    } 
} 
char* errmsg; 
sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg); 

if(SQLITE_DONE != sqlite3_step(statement)){ 
    NSLog(@"Error while updating. %s", sqlite3_errmsg(database)); 
} 
else{ 
    sqlite3_reset(statement); 
} 
sqlite3_finalize(statement); 
sqlite3_close(database); 
+0

你沒有任何日誌消息?也許你必須在你的查詢中引用AppEndTime的值。 – slecorne

回答

0

這裏是我的代碼更新創建的表。看看希望它會幫助你。

-(BOOL)updateMyTable{ 
BOOL isGood = YES; 
@try { 

    NSString *fName = [user_dic valueForKey:@"fName"]; 

    NSString *lName = [user_dic valueForKey:@"lName"]; 

    NSString *email_id = [user_dic valueForKey:@"email_id"]; 

    NSString *employee_id = [user_dic valueForKey:@"employee_id"]; 

    fName= [fName stringByReplacingOccurrencesOfString:@"'" withString:@"''"]; 
    lName= [lName stringByReplacingOccurrencesOfString:@"'" withString:@"''"]; 
    email_id= [email_id stringByReplacingOccurrencesOfString:@"'" withString:@"''"]; 
    employee_id= [employee_id stringByReplacingOccurrencesOfString:@"'" withString:@"''"]; 
    // NSString *autoId = [user_dic valueForKey:@"autoId"]; 

    NSString *sql = [NSString stringWithFormat:@"UPDATE tbl_profile SET fName = '%@', lName = '%@', email_id = '%@' ,employee_id = '%@' WHERE autoId = '%@'",fName,lName,email_id,employee_id,autoId]; 

    [database executeUpdate:sql,nil]; 
    if (MyDelegate.isLogging) { 
     NSLog(@"edit user QUERY---- >>>>%@",sql); 
     NSLog(@"edit user RESULT CODE ---- >>>>%d:", [database lastErrorCode]); 
     NSLog(@"edit user RESULT ERROR MESSAGE ---- >>>>%@",[database lastErrorMessage]); 
    } 

}@catch (NSException *exception) { 
    isGood = NO; 
} 
@finally { 

} 

return isGood; 
1

請替換您的代碼,如下所示。我希望它能解決您的問題。

if (sqlite3_prepare_v2(database, sql, -1, &Statement1, NULL) == SQLITE_OK) { 

     sqlite3_bind_text(Statement1, 1, [status UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(Statement1, 2, [messageID UTF8String], -1, NULL); 

     int success = sqlite3_step(Statement1); 
     if(success != SQLITE_ERROR) 
     { 
     // NSLog(@"Success"); 
     } 

     sqlite3_finalize(Statement1); 
    } 
+0

感謝您的回覆。但你回答不能reslove我的問題你可以PLZ編輯我的代碼,這樣我可以更好地理解 – lreddy

0

請確保您的Sqlite文件位於文檔目錄中,而不是在項目文件夾中。 更新和插入將工作當且僅當該文件在文件目錄

see this answer