0
我已经编写了一个使用Sqlite数据库的简单应用程序。它在iPhone模拟器上效果很好,但不适用于我的iPhone。在Xcode中Sqlite无法在实际设备上工作(在我的iPhone上)
-(NSString *) getFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,YES);
NSString *documentsDir=[paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"database.sql"];
}
-(void)openDatabase
{
//Open
if (sqlite3_open([[self getFilePath] UTF8String], &db) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Database failed to open.");
}
}
输出启动应用程序后:
2013-03-07 02:12:16.525 SqliteWorkApp[464:907] *** Assertion failure in -[SqliteWorkAppViewController insertRecord], /Users/cmltkt/Objective-C Apps/SqliteWorkApp/SqliteWorkApp/SqliteWorkAppViewController.m:77
2013-03-07 02:12:16.529 SqliteWorkApp[464:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error updating table.'
*** First throw call stack:
(0x3398e2a3 0x3b82797f 0x3398e15d 0x34263ab7 0x19b3b 0x195bd 0x357b5595 0x357f5d79 0x357f1aed 0x358331e9 0x357f683f 0x357ee84b 0x35796c39 0x357966cd 0x3579611b 0x374885a3 0x374881d3 0x33963173 0x33963117 0x33961f99 0x338d4ebd 0x338d4d49 0x357ed485 0x357ea301 0x19147 0x3bc5eb20)
libc++abi.dylib: terminate called throwing an exception
insertRecord功能:
-(void)insertRecord
{
NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO 'countries' ('name', 'flag') " "VALUES ('Sample Data','Sample Data')"];
char *err;
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err)
!= SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Error updating table.");
}
}
错误出现在你的'insertRecord'方法中。显示该方法的代码。 – rmaddy 2013-03-07 00:26:45
另外,设备上的文档目录中是否存在实际的文件?正如所写,您的应用程序只是打开一个空的(不存在的)数据库。您需要复制预制数据库文件和所有空表,或者需要执行SQL以创建表。 – rmaddy 2013-03-07 00:28:38
@rmaddy说得很好;除非在软件包中包含预配置的数据库,否则首先需要创建数据库及其模式。 – 2013-03-07 00:33:08