views:

42

answers:

1

Hi! I'm having a problem with some sqlite code for an iPhone program in Xcode. I was opening my database like this:

int result = sqlite3_open("stealtown.db", &database);

Which is how they had it in a book I was looking at while I type the program. But then, that way of opening a database it only works when you run in simulator, not on device. So I finally figure out I need to do this:

NSString *file = [[NSBundle mainBundle] pathForResource:@"stealtown" ofType:@"db"]; int result = sqlite3_open([file UTF8String], &database);

And that works on device, EXCEPT one thing: Each time you launch the program, it starts as if you had never created the database, and when you stick an entry in the table, it's the ONLY entry in that table.

When I used the first code on the simulator, I could open my program 6 times, each time adding 1 entry to a table, and at the end, I had 6 entries in that table. With the second code, I do exact same thing but each time there is only 1 entry in that table. Am I explaining this okay, I hope so, it's hard sometimes for me.

Anyone maybe know why this would be?

+2  A: 

On the device, the app bundle is not writable. Therefore, your database file cannot be changed. You have to create the database in your app's Documents directory on first launch. Or, if your app comes with prepopulated data, you have to copy the database file from your app bundle to the Documents directory first and then open the copy.

Ole Begemann
Thanks, this is right and fixed it!
Cocorico