views:

342

answers:

2

Something really weird has happened to my source code. The application always built successfully, and I always used to be able to launch the application. However recently, the app crashes when I launch it and return the following error in the console:

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: (

I don't understand. The app would always launch successfully with no error. But now I get this error. I had backed up my source code along the way and now when I build those older versions, I get the same error. How is this even possible. Can code go bad? If I was able to successfully launch an application before and I go back today and recompile it and it returns an error...THE SAME PIECE OF CODE?

The error seems to point to this piece of code in my AppDelegate file:

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;

}

It seems to be a problem with the database connection, but I'm still confused over how this is all possible. Please help...I've already waisted six hours debugging, with little success

A: 

well, you know exactly what line is failing: it's the call to sqlite3_prepare_v2. It looks like you're not seeing the error message, so add a line like

NSLog(@"SQL error message: %s", sqlite3_errmsg(db));

right before the assert and maybe that will tell you what's wrong.

David Maymudes
"SQL error message: no such table: dict" hmmm...very interesting...what are the possibilites
Kulpreet
i just checked my pedict.db file and there is a table named dict
Kulpreet
still no luck here
Kulpreet
perhaps you're connecting to the wrong database? a file in the wrong directory?
David Maymudes
perhaps the database file isn't getting copied to your device correctly?
David Maymudes
Thanx, I solved the problem. Seems like the files I was using became corrupted by XCode. I ended creating a new project and copying all of the files over. Seemed to work.
Kulpreet
A: 

Did you change the database by any chance? I usually get this error if I've changed the database in my project but have not done a fresh install of the app on the simulator/device.

If you are using the Apple sample app for database operations, it makes a copy of the database in the Documents folder the first time the app is run and does all operations on this database. If you change your db file, you must delete the app from your device/simulator and then do a fresh install.

lostInTransit
thanx, it seems my XCode project became corrupt with the simulator. I simply create a new project and copied all of the files over and it seems to work.
Kulpreet