1
我嘗試更新像這樣在我的表中的條目:我的更新語句不工作
NSFileManager *fileMgr=[NSFileManager defaultManager];
NSString *dbPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"quizDB.sqlite"];
if ([fileMgr fileExistsAtPath:dbPath]) {
NSLog(@"DB does exist!");//displayed
}
const char *dbpath = [dbPath UTF8String];
sqlite3_stmt *updateStmt;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSLog(@"%i",id_quiz);//displayed, in a test case it display 1 which is a quiz ID in the table
NSString *querySql=[NSString stringWithFormat:@"Update quiz set etat=1 where id=\"%i\"",id_quiz];
const char*sql=[querySql UTF8String];
sqlite3_prepare_v2(contactDB, sql, -1, &updateStmt, NULL);
if(SQLITE_DONE != sqlite3_step(updateStmt))
{
NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(contactDB));
}
else{
sqlite3_reset(updateStmt);
NSLog(@"Update done successfully!");//this is also displayed, i guess everything is ok then and the entry is updated
}
sqlite3_finalize(updateStmt);
sqlite3_close(contactDB);
}
在日誌中,我得到的消息:Update done successfully!
,所以我認爲一切,但是當我檢查數據庫在Mozilla Firefox中的SQLite Manager中,條目不會更新。我錯過了什麼嗎? thanx的幫助
嗨Piyush Patel:什麼時候checkAndCreateDatabase被調用? – Luca 2012-03-19 12:17:33
好吧,我到目前爲止做的是,我把'checkAndCreateDatabase'方法放在'AppDelegate'文件中,並在'didFinishLaunchingWithOptions'方法中調用它,但是沒有奏效。我的SQLite數據庫文件位於'Supporting Files'目錄下,你想通過'檢查你的數據庫保存在documentdir中而不是資源'Thanx來澄清:) – Luca 2012-03-19 12:33:02
它不起作用:(條目沒有更新,我當你說'檢查你的數據庫保存在documentdir中而不是資源中'時你不理解你 - 我的SQLite數據庫文件目前在Xcode的'Supporting Files'目錄下,不應該在那裏嗎? – Luca 2012-03-19 12:40:01