views:

287

answers:

2

Hi, In Android when you upgrade a database all data is lost. I'm going through a phase of development right now that is forcing many database upgrades. I don't want to lose all my data (and have to manually re-enter it) each time I upgrade my database. I would like to store my data in flat files and load those flat files into their respective tables each time a database upgrade occurs.

My questions is: What is the best way to go about this on the Android platform? Where should I store the data files (res/raw???) What sql should I execute to load these files?

Thanks.

+1  A: 

I'd put the flat files in res/raw unless they are XML, in which case I'd put them in res/xml.

As for inserting the data in the database, I'd just loop through the values in the flat files and do an insert for each row. At quick glance I don't see any batch insert methods available in Android. Just read your flat files in using a FileReader or a XmlPullParser depending on the file type.

mbaird
If your files are in a .csv format can't you just use an import statement or something to import the entire file to a table? ...that is, instead of inserting each row independently.
MoMo
+1  A: 

mbaird has a good answer for storing the flat files. But in your case (if I understand you correctly) you can get around it by implementing SQLiteOpenHelper. Just store the database version and pass it to the constructor. Then handle any changes in onUpgrade(). You won't lose all your data.

wf
I wasn't aware of SQLiteOpenHelper. That's good to know.
mbaird