【问题标题】:iphone sqlite database paths issueiphone sqlite数据库路径问题
【发布时间】:2012-07-06 18:35:16
【问题描述】:

我在我的 iPhone 应用程序中维护了一个 sqlite 数据库,所以我想知道你如何才能保留一个版本并更新一个版本。

它说如果要对数据库进行更新,您应该将其复制到文档文件夹

在我的 iPhone 模拟器上,每次打开应用程序时,文档文件夹都会发生变化

如果对其进行了复制和更改.. 应用程序如何将更新反映到我的应用程序包中的数据库 因此,当应用程序再次启动时,应用程序再次将数据库从我的包复制到文档文件夹,以便能够对其进行更改..

我的问题是有时我进行更改并再次加载应用程序发现更改已消失!

我所做的一切:

  1. 使用终端在某个路径上创建数据库,并在 xcode 中的资源文件夹上右键单击添加现有文件
  2. 我每次加载应用时都会这样做:
-(void)DBinit
{
    // Setup some globals
    databaseName = @"articlesdb.sql";

    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] copy];
    NSLog(databasePath);
    // Execute the "checkAndCreateDatabase" function
    [self checkAndCreateDatabase];  
}

-(void)checkAndCreateDatabase
{
    // Check if the SQL database has already been saved to the users phone, if not then copy it over
    BOOL success;

    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:databasePath];

    // If the database already exists then return without doing anything
    if(success) 
    {
        [fileManager removeItemAtPath:databasePath error:nil];
        return;
    }

    // If not then proceed to copy the database from the application to the users filesystem

    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

    // Copy the database from the package to the users filesystem
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

    fileManager release];
}

【问题讨论】:

    标签: iphone database sqlite path


    【解决方案1】:

    Xcode 与 iPhone 模拟器一起为您的应用程序的每个构建创建一个新的应用程序文件夹,并将旧的文档文件夹及其内容移动到新的应用程序文件夹中。您可以检查文档文件夹的内容,并决定您的 Bundle 中的文件是否比文档文件夹中的文件更适合使用。你应该明智而不是轻率地做出这个决定。

    这是正确的行为。您的软件的用户可能对数据库进行了一些重大更改和修改。如果在升级以修复小错误甚至功能增强时,他们的数据库更改、高分、修改后的图像或您的应用保存的任何内容突然被通用版本替换,他们可能不会喜欢它。

    您应该做的是比较新数据库中的数据与用户拥有的数据,对于 SQLite,您应该将用户的条目导入或复制到新数据库中,然后您可以移动您的将新数据库移至文档文件夹,删除用户但将其替换为包含用户数据和新数据的数据库。

    简而言之:确保不要用捆绑包中的新数据库替换现有数据库,因为这会破坏用户的更改。

    【讨论】:

    • 感谢您的帮助和时间我想做的是在用户对文档文件夹中的数据库进行更改之后。我希望将其复制(进行更改)到捆绑包中的数据库..
    • 或者自动发生
    • 捆绑包始终是只读的。没有任何东西可以复制回其中。这就是为什么你必须将数据库复制到下载文件夹,否则数据库是只读的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2013-04-14
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    相关资源
    最近更新 更多