【问题标题】:Adding file to Documents Directory (or elsewhere)?将文件添加到文档目录(或其他地方)?
【发布时间】:2011-01-09 11:38:03
【问题描述】:

我想用一个小的 sqlite3 数据库初始化我的应用程序。我试图将它放在 Documents 目录中,但有时它以零大小复制它,有时它根本不复制它。

我应该如何使用充满信息的数据库初始化我的应用程序?

谢谢

【问题讨论】:

    标签: iphone directory documents


    【解决方案1】:

    如何将其复制到您的应用程序中?
    如果您将应用程序与应用程序包中的 DB(作为资源)一起交付,则应将其从资源中复制到文档文件夹(如果它不存在)并在那里使用它:

    NSFileManager *fileManager = [[NSFileManager defaultManager] autorelease];
    BOOL exists = [fileManager fileExistsAtPath:databasePath];
    
    if (exists) {
        return;
    }
    
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    

    【讨论】:

      【解决方案2】:

      谢谢弗拉基米尔,

      这很有帮助。我做了一些更改,这是效果很好的最终结果:

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      
      NSString *documentsDirectory = [paths objectAtIndex:0];
      
      NSString *databasePath = @"personalLMS.s3db";
      
      NSFileManager *fileManager = [[NSFileManager defaultManager] autorelease];
      
      NSString * databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"personalLMS"  ofType:@"s3db"];
      
      [fileManager copyItemAtPath:databasePathFromApp toPath:[documentsDirectory stringByAppendingPathComponent:databasePath] error:nil];
      

      【讨论】:

        猜你喜欢
        • 2010-11-09
        • 2016-06-02
        • 2017-10-09
        • 2011-11-19
        • 2011-09-15
        • 1970-01-01
        • 2019-04-28
        • 2013-09-02
        • 1970-01-01
        相关资源
        最近更新 更多