【问题标题】:FMDB insert query executed but not inserting data in databaseFMDB 插入查询已执行但未在数据库中插入数据
【发布时间】:2013-04-15 00:54:47
【问题描述】:

我是 SQLITE 数据库的新手,我正在使用 FMDB。在我的应用程序中,我想将一些数据插入数据库。我使用以下代码插入数据。

-(void)insertBookmark
{

    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSString *dbFilePath = [bundlePath stringByAppendingPathComponent:@"ISGData.sqlite"];
    FMDatabase *database = [FMDatabase databaseWithPath:dbFilePath];
    [database open];
    [database executeUpdate:@"INSERT INTO bookmark (pageno) VALUES (?);",page, nil];
    NSLog(@"%@",[database lastErrorMessage]);
    [database close];

}

这里的“书签”是我的数据库中的表的名称,它有一个名为“pageno”的整数类型的实体。当我在查询应用程序崩溃中传递一个 int 值时,显示访问不正确。如果我传递一个字符串值,在我的日志中我得到“不是错误”,但这些值没有被插入到数据库中。我的代码有什么错误吗?

【问题讨论】:

    标签: iphone ios objective-c database sqlite


    【解决方案1】:

    您正在尝试修改应用程序包中的数据库。捆绑包本身是只读的。为此,您必须先将数据库文件复制到文档目录,然后打开并修改它。

    简而言之,将函数中的前三行替换为:

    FMDatabase *database = [self openDatabase];
    

    openDatabase 定义如下:how do we open an already existing database fmdb and where to include it?

    【讨论】:

      【解决方案2】:

      汉普斯是对的。您需要将其复制到文档目录中才能执行写入操作:

      BOOL success;
      NSError *error;
      
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *filePath = [documentsDirectory stringByAppendingString:@"ISGData.sqlite"];
      
      success = [fileManager fileExistsAtPath:filePath];
      if (success) return;
      
      NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"ISGData.sqlite"];
      success = [fileManager copyItemAtPath:path toPath:filePath error:&error];
      
      if (!success) {
          NSAssert1(0, @"Failed to copy DB. Error %@", [error localizedDescription]);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-29
        • 1970-01-01
        • 2012-04-23
        • 1970-01-01
        • 2023-01-11
        • 1970-01-01
        • 2018-09-15
        • 1970-01-01
        相关资源
        最近更新 更多