gpt4 book ai didi

ios - 将 sqlite 数据库重置为默认值

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:57 27 4
gpt4 key购买 nike

我正在我的应用程序中编写一个重置函数,它将使用一些预填充的数据将我的 sqlite 数据库恢复到其原始状态。下面的代码从文档目录中删除数据库并从包中复制数据库,但数据仍然出现在旧数据库中?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MoneyMonthly.sqlite"];

if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){

[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];

}

BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error;

NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"MoneyMonthly.sqlite"];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MoneyMonthly.sqlite"];

success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

if (!success) {

NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}

最佳答案

我在我的一个应用程序中执行此操作。我在我的应用程序包中保留了一个预构建的数据库文件。该数据库包含所有表和样本数据。

当我需要重置用户的数据库时,我将其关闭,然后用 bundle 中的干净副本覆盖数据库。

关于ios - 将 sqlite 数据库重置为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13335761/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com