【问题标题】:sqlite database file doesn't update in iOSsqlite 数据库文件不会在 iOS 中更新
【发布时间】:2015-08-06 14:33:02
【问题描述】:

我有一个使用 sqlite 的 iOS 应用程序,sqlite 数据库文件已预先配置并添加到 xcode 中。一切都很好,直到我需要在这个 sqlite 文件中添加一个名为“activities”的新表,所以我添加了数据库文件的表,替换了 xcode 中的数据库文件,现在在模拟器或真正的 iOS 设备上运行时,它一直抱怨“没有这样的桌子:活动”。即使我在我的设备或模拟器上删除以前安装的应用程序也不起作用。那里发生了什么?顺便说一句,下面是我每次实例化我的 DatabaseManager 时处理数据库文件的代码:

- (instancetype)initWithDatabaseFilename:(NSString *)dbFilename {
    self = [super init];
    if (self) {
        // Set the documents directory path to the documentsDirectory property.
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        self.documentsDirectory = [paths objectAtIndex:0];

        // Keep the database filename.
        self.databaseFilename = dbFilename;

        // Copy the database file into the documents directory if necessary.
        [self copyDatabaseIntoDocumentsDirectory];
    }
    return self;
}

- (void)copyDatabaseIntoDocumentsDirectory {
    // Check if the database file exists in the documents directory.
    NSString *destinationPath = [self.documentsDirectory stringByAppendingPathComponent:self.databaseFilename];
    if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
        NSLog(@"file not exists");
        // The database file does not exist in the documents directory, so copy it from the main bundle now.
        NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.databaseFilename];
        NSError *error;
        [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destinationPath error:&error];

        // Check if any error occurred during copying and display it.
        if (error != nil) {
            NSLog(@"%@", [error localizedDescription]);
        }
    } else {
        NSLog(@"file is already there");
    }
}

【问题讨论】:

  • 嗯,事实证明,除了删除模拟器/设备上的应用程序之外,我需要的额外步骤是 CLEAN Xcode 以重建项目,然后一切都很好......
  • 这是 Xcode 中的一个常见错误,在核心数据方案中也会发生同样的事情。当事情没有按预期工作时,请始终尝试清理工作区和临时构建目录。

标签: ios sqlite


【解决方案1】:

通过copyDatabaseIntoDocumentsDirectory方法中生成的终端路径从文档目录中删除数据库文件在项目中拖放数据库文件。确保通过终端创建表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多