所以我一直在這個項目上工作了一段時間。我從數據庫讀取數據並將其格式化爲UITableViews並沒有問題。但現在我也想寫入數據庫。問題是我不斷從sqlite中獲取「數據庫已鎖定」錯誤。在完成了原始版本之後,我通過實現我的數據庫處於捆綁狀態並因此不可寫,從而獲得了面部掌紋時刻。因此,我將數據庫重新定位到可以寫入的Apps Documents文件夾。但現在我仍然得到相同的「數據庫被鎖定」sql錯誤。我只在必要時打開和關閉數據庫。據我所知,我不會在任何地方打開它。以下是我想要更新的代碼。有什麼想法嗎?iPhone sqlite3鎖定,只能讀取,不能寫入
- (BOOL) loanBookTo:(NSString *)newborrower{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"books.sqlite"];
if([[NSFileManager defaultManager] fileExistsAtPath:path]){
NSLog(@"File Exists at: %@", path);
}
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
NSString *mySQL = @"UPDATE BOOKS SET LOANED = 1, BORROWER = \"<BORROWER>\" where ISBN = \"<ISBN>\"";
mySQL = [mySQL stringByReplacingOccurrencesOfString:@"<ISBN>" withString:self.isbn];
mySQL = [mySQL stringByReplacingOccurrencesOfString:@"<BORROWER>" withString:newborrower];
const char *sql = [mySQL UTF8String];
char* errmsg;
sqlite3_exec(database, sql, NULL, NULL, &errmsg);
// Q. Database is locked. Why?
// A. Because it is in the Bundle and is ReadOnly.
// Need to write a copy to the Doc folder.
// Database is Locked error gets spit out here.
printf(errmsg);
sqlite3_close(database);
}
return NO;
}
數據庫在應用程序包或文檔目錄中位於何處? – zaph 2009-10-31 15:27:50