【问题标题】:ios: Where to put the Sqlite file in solution?ios:将 Sqlite 文件放在解决方案中的什么位置?
【发布时间】:2014-10-26 23:35:45
【问题描述】:

我是 iOS 开发的新手,我正在对使用 Sqlite 的应用程序进行更改。我要在一些表中添加一些新字段,我用软件浏览了数据库并在其中添加了新字段 inventory_db_src.sqlite 但是当我在模拟器中看到它使用 inventory_db.sqlite 这很奇怪,因为解决方案中没有 inventory_db.sqlite 文件,也没有通过 SQL 脚本创建数据库的代码。如果我调试代码,它会成功获得 inventory_db.sqlite 路径,并且永远不会执行 inventory_db_src.sqlite 行并将inventory_db.sqlite 放入我的新字段不存在的模拟器中这些在 inventory_db_src.sqlite 中。请帮帮我

BOOL success;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"inventory_db.sqlite"];
        //      [fileManager removeItemAtPath:dbPath error:nil];
        success = [fileManager fileExistsAtPath:dbPath];
        success = NO;

        if (success) {
            int savedVersion = [[NSUserDefaults standardUserDefaults] integerForKey:kVERSION_KEY];
            if (kCURRENT_DB_VERSION != savedVersion) {
                [fileManager removeItemAtPath:dbPath error:nil];
                success = NO;
            }
        }
        if (!success) {
            // The writable database does not exist, so copy the default to the appropriate location.
            NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"inventory_db_src.sqlite"];
            success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
            if (!success) {
                NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
            }
        }

        _INVENTORY_DB = [[FMDatabase alloc] initWithPath:dbPath]; 

【问题讨论】:

    标签: ios objective-c iphone sqlite ipad


    【解决方案1】:

    上面代码中的第 6 行将名称显示为 inventory_db.sqlite,这就是您的应用将使用的文件。您需要修改它以使用新的数据库名称。请注意,通过修改您的数据库,您可能会得到意想不到的结果,因此您需要管理您的应用程序使用的数据库版本并根据需要进行数据更正。这是 Core Data 可以提供帮助的一项不错的功能。

    【讨论】:

      【解决方案2】:

      根据您的说法,旧数据库似乎已缓存到您的模拟器中。请通过

      重置你的模拟器

      iOS 模拟器 -> 重置内容和设置

      它应该可以工作......

      【讨论】:

        猜你喜欢
        • 2011-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-13
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        相关资源
        最近更新 更多