在设备上测试sqlite数据库应用程序

时间:2011-06-11 04:38:06

标签: iphone sqlite

我正在开发一个在模拟器中工作正常的数据库应用程序(插入,更新,正确删除所有功能)。尝试在iPhone上安装/运行应用程序时,我的问题就出现了。

在初始运行时,程序会在应用程序的文档目录中创建数据库的可编辑版本。在应用程序包中找不到数据库时,我的测试失败。数据库已添加到XCode中的Resources文件夹中。

检查现有数据库版本的代码(加上几个NSLog语句),如果还不存在则创建一个代码如下:

- (NSString *)createWriteableDatabase {     BOOL databaseExists;     NSError *错误;

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *writeableDB = [documentsDir stringByAppendingPathComponent:@"flashCard.db"];

NSLog(@"writeableDB = '%@'.", writeableDB);

databaseExists = [fileManager fileExistsAtPath:writeableDB];

NSLog(@"databaseExists = %d.", databaseExists);

if (databaseExists)
    return writeableDB;

//--- Writeable version of database does not exist -- copy from bundled software. ---

NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] 
                         stringByAppendingPathComponent:@"flashCard.db"];

NSLog(@"defaultPath = '%@'.", defaultPath);

databaseExists = [fileManager fileExistsAtPath:defaultPath];

NSLog(@"databaseExists = %d.", databaseExists);

databaseExists = [fileManager copyItemAtPath:defaultPath toPath:writeableDB error:&error];

if (!databaseExists)
{
    NSAssert1(0, @"Failed to create writeable database file: '%@'.", [error localizedDescription]);
    return @"";
}

return writeableDB;

}

调试器中显示的消息如下:

警告:无法读取/ XCode 3.2.5 / Platforms / iPhoneOS.platform / DeviceSupport / 4.3.3(8J2)/Symbols/System/Library/AccessibilityBundles/AccessibilitySettingsLoader.bundle/AccessibilitySettingsLoader(找不到文件)的符号。 2011-06-10 21:12:11.130 FlashEm [3768:707] writeableDB ='/var/mobile/Applications/9DBC50B5-2559-4E6A-BD21-B5B56D9DA18A/Documents/flashCard.db'。 2011-06-10 21:12:11.144 FlashEm [3768:707] databaseExists = 0。 2011-06-10 21:12:11.149 FlashEm [3768:707] defaultPath ='/ var / mobile / Applications / 9DBC50B5-2559-4E6A-BD21-B5B56D9DA18A / FlashEm.app / flashCard.db'。 2011-06-10 21:12:11.154 FlashEm [3768:707] databaseExists = 0。 2011-06-10 21:12:11.169 FlashEm [3768:707] *断言失败 - [DBAccess createWriteableDatabase],/ Users / horsethoner1234 /Projects /FlashEm / Class /../DBAccess.m:92 2011-06-10 21:12:11.201 FlashEm [3768:707] * 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法创建可写数据库文件:'操作无法完成。没有这样的文件或目录'。'

提前感谢您提供给我的任何帮助,如果我的问题文字格式不正确,我会道歉 - 这是我第一次在本网站上提问。

(我查看了另外两个问题,其中发生了同样的问题,并检查了他们是否适用于我的情况,但他们没有。)

1 个答案:

答案 0 :(得分:0)

您的崩溃报告说您的数据库文件找不到您的文档目录。只有当您没有在资源文件夹中正确安装数据库文件意味着在您的软件包中时,才会发生这种情况,因此iphone设备上的程序无法读取您的数据库文件。