【问题标题】:Copy local SQLite Database to iPhone?将本地 SQLite 数据库复制到 iPhone?
【发布时间】:2010-02-14 16:57:47
【问题描述】:

我正在用 coredata 和 Objective-c 做一些实验。 我的应用程序在模拟器中运行良好。

它也适用于我的 iPodTouch。 但是如何将我的本地数据库复制到我的设备上?

谢谢,

【问题讨论】:

    标签: iphone objective-c sqlite core-data


    【解决方案1】:

    将 sqlite 文件从模拟器目录结构中复制出来(在 ~/Library/Application Support/iPhone Simulator 下)并将其放入您的项目中以包含在 Resources 目录中。到达那里后,您可以:

    1. 在运行时将文件复制到您的文档目录以使其可写;或
    2. 直接从应用程序包中引用它,使其成为只读数据库

    取决于您的应用需求。

    【讨论】:

    • 听起来不错。但是如何直接从应用程序包中引用它以使其成为只读数据库?
    • 我试过这个。但它确实有效:-( NSURL *storeUrl = [NSURL fileURLWithPath:@"bundle://Locations.sqlite"]; [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]
    • [[NSBundle mainBundle] pathForResource:@"Locations" ofType:@"sqlite"] 会给你一个字符串路径。然后使用 [NSURL fileURLWithPath:] 将其转换为 URL 将返回您传递给 Core Data 的 url。
    【解决方案2】:

    这就是解决方案(感谢 Marcus S. Zarra)

    改变这个

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Locations.sqlite"]]
    

    进入这个:

    NSURL *storeUrl = [NSURL fileURLWithPath: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"Locations.sqlite"]];
    

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多