所以我在这个项目上工作了一段时间。我从数据库读取数据并将其格式化为 UITableViews 没有问题。但现在我也想写入数据库。问题是我不断从 sqlite 收到“数据库已锁定”错误。在弄乱了原始版本之后,我意识到我的数据库在捆绑包中,因此不可写,从而获得了面对面的时刻。所以我将数据库重新定位到可写的 Apps Documents 文件夹。但现在我仍然得到相同的“数据库已锁定”sql 错误。我只在必要时打开和关闭数据库。据我所知,我不会让它在任何地方打开。下面是我想要更新的代码。有什么想法吗?
- (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;
}