views:

123

answers:

1

Hi guys

I have a question, I can't get my sqlite database to work/open on an ipad device. Everything works fine on the simulator but not a real device. I put MODE to NO when i want to test on a device but it fails to load.

    sqlite3 *database;
if (MODE) { //Simulator
    result = sqlite3_open("/Users/userID/Myapp/VRdb.sqlite", &database);
} else { // On device
    result = sqlite3_open([databasePath UTF8String], &database);
}

if(result != SQLITE_OK)
{
    sqlite3_close(database);
    view = [[UIAlertView alloc]
            initWithTitle: @"Database Error"
            message: @"Failed to open database."
            delegate: self
            cancelButtonTitle: @"OK" otherButtonTitles: nil];
    [view show];
    [view autorelease];

}

databasePath is obtained by :

databasePath = [[NSBundle mainBundle] pathForResource:@"VRdb" ofType:@"sqlite"];

Is there something that I missed?????

+2  A: 

Is there actually a file at that path? Though the sqlite3_open() function will create the file if it doesn't exist, you can't create files inside the application bundle (permissions). The file actually has to already exist and be copied into the bundle as a resource at build time, then used as a template to copy to a writeable area like the documents folder.

(thanks to Peter for prompting me to realize my answer was incomplete :-) )

Joshua Nozzi
awlcs: The best way, if you intend to create a writeable database, would be to get the path to the Documents directory and use that. You can and should do that unconditionally, without regard to whether you are running on the Simulator or not. Indeed, I would suggest not creating any more differences between Simulator behavior and device behavior than you face anyway.
Peter Hosey
Thanks to both of you, i'll give a try. I thought that using NSBundle would have been enough but I was wrong :)
okayasu