I am using SQLiteOpenHelper class to create, open database. for my application i am creating writable object SQLiteDatabase which i am using to read and write data to database. This object is static for main class and used in all application to read write in to database. my application is working properly on emulator. but on device after some read write query fires. why it is happening, please if any one has solution help me.
2 Answers
It is not recommended to have the helper as a static instance. Rather you should instanciate it each time you need to acces the DB. What might be happening is that your DB connection is getting closed by Android (because it needs memory, because there are too many open connections, ...) and when you do a query, you do not check that the connection is still open.
You can read this tutorial to get the idea of how to do it properly.
Basically inside an activity, the idea:
private void reloadData() {
MyDBHelper db = new MyDBHelper(this.getApplicationContext());
db.open();
Cursor c = db.query(...);
db.close();
// Update your data using the cursor
}

- 28,208
- 16
- 81
- 124
I had the same problem a few weeks ago, and I found this page, it's exactly what's wrong. The SQLite is buggy in Android and is implemented the wrong way, so you can't have threads reading and writing to the database/table at the same time.
setLockingEnabled() just dont work the way it's supposed.
What I did was running all methods that read and write to the db "Synchronized" which means you'll never have a problem with reading or writing data at the same time no matter how many threads you have.
Regards Tobias

- 873
- 9
- 17