Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tazboy

macrumors newbie
Original poster
Apr 6, 2010
8
0
Something is not correct in my function because it is successfully opening a file that does not exist. Can someone please help me with where I might be going wrong?

Code:
- (void) initializeDatabase {
	
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"[B]game.sqlite[/B]"];
	
  int result = sqlite3_open([path UTF8String], &db);
	
  if (result != SQLITE_OK){
    sqlite3_close(db);
    alert = [[UIAlertView alloc] initWithTitle:@"Database error"
 					      message:@"Failed to open database"
				              delegate:self
				   cancelButtonTitle:@"Hrm" 
				   otherButtonTitles:nil];
    [alert show];
    [alert autorelease];
    return;
}
	
  alert = [[UIAlertView alloc] initWithTitle:@"Database good to go" 
					    message:@"Database was opened"
					    delegate:self
				 cancelButtonTitle:@"Yeppers" 
				 otherButtonTitles:nil];
  [alert show];
  [alert autorelease];
}

I don't even have a file named "game.sqlite", yet it apparently opens it. Does someone have any idea why this is happening?

Thanks for any information.
 
You're using the old sqlite3_open() call. The behavior of that call is to open the database if it exists, or create it if it doesn't exist.

If you need more control you should use the newer sqlite3_open_v2() call instead, it lets you specify what should happen if the database file doesn't exist.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.