Database corruption can also happen when you open the same database file 
multiple times simultaneously. When the database is broken SQLiteDatabase 
simply re-creates the database file by default. I think that is largely 
fixed in newer Android versions and you would get a DatabaseLockedException 
if you try to open it multiple times.

Make sure to:

   1. Only use a single SQLiteDatabase connection / SQliteOpenHelper per 
   database file
   2. Open / close that connection at appropriate times in your activity 
   life cycle.

About 2.:

If you create the connection in onCreate / onResume and close it in onStop 
or onDestroy for example it is very likely that you'll end up with two open 
SQLiteDatabase connection objects simultaneously because two activities are 
likely to intersect in that case. You could probably get around that by 
opening the connection object in onResume and closing it in onPause.

Alternatively you can keep your database connection object as a singleton 
in your application object for example. Keep a reference counter there too. 
Every time when your activity starts it can request the db connection and 
increment the reference counter by one. When your activity is done 
(onDestroy) it can decrement the counter. If the counter hits zero you can 
close the db connection.

On Wednesday, August 8, 2012 5:44:20 AM UTC-5, Abhi wrote:
>
> Hello,
>
> I am developing an app. It is working fine but if i left the app open for 
> few hours the database gets blank sometimes(1 out of 5). I tried hard to 
> track the issue but couldn't found any. I am using Sqlite database. Please 
> help me as soon as possible.
>
> Thanks
> Abhi
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to