【问题标题】:Objective-C: coping and deleting files (FileManager)Objective-C:复制和删除文件(文件管理器)
【发布时间】:2014-07-13 01:35:04
【问题描述】:

我正在使用 FMDatabase 处理一个 sqlite 数据库应用程序。

我想更新位于主包中的数据库表

NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:db_name];

包含位于文档目录中的数据库中的数据

NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *db_Path = [docsPath stringByAppendingPathComponent:db_name];

我多次尝试在“默认”数据库上执行 UPDATE 查询,但它说它已被锁定。所以我考虑在文档目录中使用名称 db_name_cp 处理它并对其进行处理。

NSString *cpPath = [documentsDirectory stringByAppendingPathComponent:@"db_name_cp.sqlite"];
        success = [fileManager fileExistsAtPath:cpPath];
        if (success){
            //remove the existing one
            [fileManager removeItemAtPath:cpPath error:&error];
        }
        //create the copy of the "main bundle" database
        [fileManager copyItemAtPath:default_database.dbPath toPath:cpPath error:&error];

然后我尝试对复制的数据库执行 UPDATE,但如果我对其执行 SELECT,它不会更新,如果我在 phpMyAdmin 或 SQLiteManager 上运行查询,它会更新记录。

有什么想法吗?也许我在设备目录上处理它们是错误的。

非常感谢

【问题讨论】:

  • 您不能对应用中的任何内容进行更改,这显然包括应用的 mainBundle。
  • 是的,我用来对存储在 doc 目录中的数据库执行 UPDATE 查询,它们工作正常。

标签: ios objective-c sqlite nsfilemanager fmdb


【解决方案1】:

您不能对应用程序中的任何内容进行更改,这显然包括应用程序的 mainBundle,它是只读且已签名的。

Documents 目录很好,它是要使用的正确目录。

如果需要合并则合并到 Documents 目录 DB。或者在第一次启动时将 bundle DB 复制到 Documents 目录,然后合并到其中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多