【问题标题】:sqlite database not updating modified data in iphone sdksqlite数据库不更新iphone sdk中的修改数据
【发布时间】: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 添加实现。

标签: iphone sqlite


【解决方案1】:

您的代码没有显示打开数据库,但我猜您正在打开一个包含在您的应用程序包中的数据库?如果是这样,您首先需要将数据库复制到应用程序的 Documents 目录中,因为应用程序包是只读的。

您可以通过以下方式获取 Documents 目录的路径:

[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]

【讨论】:

  • 是的,我已经这样做了,就好像我插入了一个保存在数据库中的新数据但是如果我尝试修改现有数据然后保存它它不起作用我得到相同的旧数据正在尝试修改...
  • 我尝试使用断点,它在:[objectToEdit setValue:txtField.text forKey:self.keyOfTheFieldToEdit];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多