我的源代码发生了一些非常奇怪的事情。应用程序总是成功构建,我总是习惯于启动应用程序。但是最近,当我启动它时,应用程序崩溃并在控制台中返回以下错误:
2009-07-23 20:30:06.390 App[15652:20b] Database Successfully Opened :)
2009-07-23 20:30:06.393 App[15652:20b] *** Assertion failure in -[AppDelegate initializeTableData], /Users/me/Desktop/App/Classes/AppDelegate.m:39
2009-07-23 20:30:06.394 App[15652:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error preparing statement'
2009-07-23 20:30:06.394 App[15652:20b] Stack: (
我不明白。该应用程序将始终成功启动,没有错误。但现在我收到了这个错误。我一直备份我的源代码,现在当我构建那些旧版本时,我得到了同样的错误。这怎么可能呢?代码可以变坏吗?如果我之前能够成功启动一个应用程序,我今天就回去重新编译它会返回一个错误......相同的代码片段?
错误似乎指向我的AppDelegate文件中的这段代码:
enter code -(void)initializeTableData {
entries = [[NSMutableArray alloc] init];
sqlite3 *db = [AppDelegate getNewDBConnection];
sqlite3_stmt *statement = nil;
const char *sql = "select * from dict";
if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL) != SQLITE_OK)
NSAssert1(0,@"Error preparing statement", sqlite3_errmsg(db));
else
{
while(sqlite3_step(statement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aGurmukhi = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
NSString *aShahmukhi = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
NSString *aPOS = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
NSString *aEnglish = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
// Create a new animal object with the data from the database
Entry *entry = [[Entry alloc] initWithName:aGurmukhi shahmukhi:aShahmukhi pos:aPOS english:aEnglish];
// Add the animal object to the animals Array
[entries addObject:entry];
[entry release];
}
}
//release the resources
sqlite3_finalize(statement);
}
+(sqlite3 *) getNewDBConnection{
sqlite3 *newDBconnection;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"pedict.db"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(@"Database Successfully Opened :)");
} else {
NSLog(@"Error in opening database :(");
}
return newDBconnection;
}
这似乎是数据库连接的一个问题,但我仍然对如何做到这一点感到困惑。请帮忙......我已经等了六个小时调试,收效甚微
答案 0 :(得分:0)
嗯,你确切地知道哪条线路失败了:它是对sqlite3_prepare_v2的调用。看起来您没有看到错误消息,因此添加一行如
NSLog(@"SQL error message: %s", sqlite3_errmsg(db));
在断言之前,也许这会告诉你什么是错的。
答案 1 :(得分:0)
您是否有机会更改数据库?如果我在项目中更改了数据库但没有在模拟器/设备上重新安装应用程序,我通常会收到此错误。
如果您使用Apple示例应用程序进行数据库操作,它会在第一次运行应用程序时在Documents文件夹中创建数据库的副本,并对此数据库执行所有操作。如果更改了db文件,则必须从设备/模拟器中删除该应用程序,然后进行全新安装。