views:

351

answers:

1

I have an app that is running in the simulator. I read and write from a sqlite3 data source. However, if i restart the app, then all datg that i had previously wrote to the db is lost. The data is always in its original state.

Now back when i was developing this app i thought i read somewhere that data can not be persisted via iphone simulator.

Can anybody confirm or deny this?

Thanks!

A: 

You need to place your db file in a writable place, e.g. in the Documents folder. All the bundle files are read-only files.

If you are distributing an initial database with the app, you will need to copy it to Documents (or another folder) and use the copy.

You also need to ensure that you close the database connection in your application is closing (i.e. you receive a applicationWillTerminate message).

notnoop
Thanks.I am copying to the documents folder. but im not certain if im closing the db properly. Let try this out and see what happens.
Edward An
OK. the only place i call sqlite3_close is when i have an error with the db initialization stuff. In my applicationWillTerminate message, the only thing i do is update all data entities to the db if they are dirty. Other than that, im not closing the db after that. Should i be?is it simply sqlite3_close(db)?
Edward An
are you sure that you don't override your writable database everytime?
notnoop
yes, im sure. database is owned by the app delegate. each data entity in my model has a reference to it.each of my transaction functions, such as createData, has a sqlite3_prepare_v2 statement and ultimately a sqlite3_reset statement.None of these functions have a sqlite3_close or a sqlite3_finalize.The only function that has both of the statements is in my initDatabase function (in the appdelegate) which also calls sqlite3_open.
Edward An
msaeed, i didnt think this was the case, but based on a NSLog i just put in, it seems you are right! I am re-writing the database. in other words, the code to detect whether it exists is not working!
Edward An
Wow. i actually had code that said "if it exists, delete it". Case solved, i hope.
Edward An