2009-06-27 71 views
1

我將數據插入表中,sqlite3_step返回SQLITE_DONE。但有時候桌子上沒有新的數據!即使我在數據添加後立即進行檢查。奇怪的SQLite行爲

這裏是我的代碼:

sqlite3_stmt *addTranslationStmt2 = NULL; 

if(sqlite3_prepare_v2(database , "INSERT INTO translations(lang1_wordid, lang2_wordid) VALUES(?, ?)" , -1, & addTranslationStmt2, NULL) != SQLITE_OK) 
{ 
    NSLog(@"statement prepare error"); 
    addTranslationStmt2 = NULL; 
    return -1; 
} 

sqlite3_bind_int(addTranslationStmt2, 1, word_id); 
sqlite3_bind_int(addTranslationStmt2, 2, translation_id); 

if(sqlite3_step(addTranslationStmt2) != SQLITE_DONE) 
{ 
    NSLog(@"addTranslationStmt2 error: '%s'", sqlite3_errmsg(database)); 
    sqlite3_reset(addTranslationStmt2); 
    return -1; 
} 

sqlite3_reset(addTranslationStmt2); 

if (![self wordHasTranslation: word_id translation_id: translation_id]) 
    NSLog(@"And after all translation was not added even though sqlite3_step returns SQLITE_DONE!"); 

所以我想知道爲什麼是被稱爲最後提到的日誌功能。這意味着添加數據後表中沒有數據。有時增加,有時不增加。

問題在哪裏? 預先感謝您。

更新: 我發現只有創建新數據庫時纔會發生這種情況。應用程序重新啓動後,它以正確的方式工作。但是如果創建一個新的數據庫,這個奇怪的行爲會回來。

+0

問題出在wordHasTranslation上嗎? – outis 2009-06-27 16:59:04

+0

不,它只是一個告訴我數據是否被添加的函數。我甚至手動檢查數據庫 - 沒有新數據的跡象。 儘管當我在代碼的其他部分插入其他數據時,一切工作都很完美。 – 2009-06-27 17:06:03

回答

2

Sqlite默認在自動提交模式下運行,但我們無法知道某些以前的交互或設置是否可能已重置;如果在INSERT之後添加明確的COMMIT,那有幫助嗎?

編輯:可能不會,從意見給予進一步的信息,但以防萬一,你就需要在其他情況下提交,最簡單的方法是:

char* errmsg; 
int result = sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg); 

與通常的結果代碼& C(你如果使用了錯誤信息,則需要使用sqlite3_free(errmsg))。