sqlite successfully opening no file?

Discussion in 'iOS Programming' started by tazboy, Apr 6, 2010.

  1. tazboy macrumors newbie

    Joined:
    Apr 6, 2010
    #1
    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.
     
  2. bredell macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #2
    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.
     

Share This Page