【问题标题】:How can i change sqlite ReadOnly to ReadWrite on the iPhone?如何在 iPhone 上将 sqlite ReadOnly 更改为 ReadWrite?
【发布时间】:2016-10-05 11:22:21
【问题描述】:

我将我的应用程序部署到我的 iPhone 并获得

Unknown error calling sqlite3_step (8: attempt to write a readonly database) eu 关于插入/更新语句。

在模拟器上一切正常。

我的 sqlite 数据库放在资源文件夹 (Xcode) 中。

感谢您的帮助!

【问题讨论】:

  • 可能你已经这样做了,请检查你的数据库的权限。

标签: iphone sqlite


【解决方案1】:

您的应用程序包在 iPhone 上不可写。您必须将该文件复制到其他位置,例如您的文档文件夹。它可以在模拟器中运行,因为 Mac 不会强制执行 iPhone 所做的所有沙盒限制。

【讨论】:

  • 好的,但是如何使用我的应用“交付”一个 sqlite 数据库?
【解决方案2】:

您可以将数据库从应用程序包目录复制到 viewDidLoad 中的 Documents 目录。在此之后,您可以在 Documents 目录中读取/写入数据库。当然,你需要在复制之前检查Documents目录中的数据库是否存在,以免下次启动应用程序时覆盖它。

假设您已在 .m 文件中定义了数据库名称 '#define kFilename @"yourdatabase.db"'。

在 viewDidLoad 中添加:


// Get the path to the main bundle resource directory.

NSString *pathsToReources = [[NSBundle mainBundle] resourcePath];

NSString *yourOriginalDatabasePath = [pathsToResources stringByAppendingPathComponent:kFilename];

// Create the path to the database in the Documents directory.

NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [pathsToDocuments objectAtIndex:0];

NSString *yourNewDatabasePath = [documentsDirectory stringByAppendingPathComponent:kFilename];

if (![[NSFileManager defaultManager] isReadableFileAtPath:yourNewDatabasePath]) {

if ([[NSFileManager defaultManager] copyItemAtPath:yourOriginalDatabasePath toPath:yourNewDatabasePath error:NULL] != YES)

NSAssert2(0, @"Fail to copy database from %@ to %@", yourOriginalDatabasePath, yourNewDatabasePath);

}

祝你好运!

aobs

【讨论】:

  • 对不起,但对我没有用:我的路径看起来像:/var/mobile/Applications/30D76248-1E01-4A60-9986-EA4417762A01/Documents/mytable.sqlite 但我不能写任何东西到数据库:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
  • 2012-05-03
  • 1970-01-01
  • 2012-07-11
  • 2012-06-30
  • 2010-10-18
相关资源
最近更新 更多