【发布时间】:2012-06-03 00:53:04
【问题描述】:
好吧,我是 iphone 编码新手,在我的一个应用程序中使用 sqlite 问题是我的 sqlite 数据库没有更新修改后的数据,我正在学习基于 sqlite 的教程:
这就是我正在做的:-
- (void) saveAllData {
if(isDirty) {
if(updateStmt == nil) {
const char *sql = "update Work Set Engagement = ?, Status = ?, Where WorkID = ?";
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(updateStmt, 1, [Engagement UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 2, [Status UTF8String], -1, SQLITE_TRANSIENT);
//sqlite3_bind_double(updateStmt, 2, [Status doubleValue]);
sqlite3_bind_int(updateStmt, 3, WorkID);
//int Success = sqlit3_step(updateStmt);
if(SQLITE_DONE != sqlite3_step(updateStmt))
NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));
sqlite3_finalize(updateStmt);
isDirty = NO;
}
我正在使用:
- (IBAction) save_Clicked:(id)sender {
//Update the value.
//Invokes the set<key> method defined in the Work Class.
[objectToEdit setValue:txtField.text forKey:self.keyOfTheFieldToEdit];
[self.navigationController popViewControllerAnimated:YES];
}
点击保存按钮:
- (void) setEngagement:(NSString *)newValue {
self.isDirty = YES;
[Engagement release];
Engagement = [newValue copy];
}
- (void) setStatus:(NSString *)newNumber {
self.isDirty = YES;
[Status release];
Status = [newNumber copy];
}
为了设定值,我撞了我的头一千次,找不到我做错了什么,请有人帮我解决这个问题.....
这是我的 getDBPath 实现的代码
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"CheckList.sqlite"];
}
此代码用于我的数据库: - (void) copyDatabaseIfNeeded {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CheckList.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
【问题讨论】:
-
database的路径是什么?不保存最常见的原因是数据库文件没有移动到用户的文档目录中。 -
我想我已经做到了,因为我可以在数据库中插入数据但不能修改它..
-
这是我所做的 .. 在我的代码中: - (void) copyDatabaseIfNeeded { NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *错误; NSString *dbPath = [self getDBPath]; BOOL 成功 = [fileManager fileExistsAtPath:dbPath]; if(!success) { NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CheckList.sqlite"];成功 = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; if (!success) NSAssert1(0, @"创建可写数据库文件失败,消息为 '%@'。", [错误本地化描述]); } }
-
请使用您的代码更新问题,而不是添加为评论。也为
getDBPath添加实现。