Chinaunix首页 | 论坛 | 博客
  • 博客访问: 297045
  • 博文数量: 69
  • 博客积分: 3093
  • 博客等级: 中校
  • 技术积分: 626
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-17 13:48
文章分类

全部博文(69)

文章存档

2011年(27)

2010年(11)

2009年(31)

分类: BSD

2011-01-14 16:49:19

解决了SQLite更新失败的问题后,项目开发顺利进行,很快就到了真机测试的阶段。

结果很不幸的,当程序安装到手机上之后,SQLite数据库又不能更新了。奇怪的是,这次经过反复检查,在模拟器里,数据库是可以顺利更新的,但是在iPhone上却不行。于是我使用NSLog打印了SQLite操作日志,发现系统报错“unable to update a read-only database”。

于是我打开iPhone文件夹,找到位于.app目录下的数据库文件,发现其属性果然为只读。经过查找资料,iPhone应用程序束中的文件全部为只读属性,不可更改。于是解决方案就很简单了,即把数据库文件拷贝到应用目录的Documents目录下就可以更新了。

于是我修改了源代码:

//获取文件管理器 NSFileManager *fm = [NSFileManager defaultManager];
//获得应用程序所在的application目录下的documents目录地址 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
//获得应用程序束(.app目录)下的数据库文件地址 NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dest = [documentsDirectory stringByAppendingPathComponent:@"try.db"];
NSString *source = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"rcmt.db"];
if(![fm fileExistsAtPath:dest]){
  [fm copyItemAtPath:source toPath:dest error:nil];
}
g_Database = [[Database alloc] initWithFile: dest];

之后,果然问题都解决了!

阅读(1747) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~