0

I have an app with a sqlite database.

I created my own database class that holds an instance of SqliteDatabase . The class implements my queries, open, close, etc. (the class is NOT a singleton).

I have an activity, a service and an appwidget in my app.

Where I need the database, I create an object of my class, open , do stuff and close at the end.

for example in the activity I open the db in onStart and close it onStop.

Everything works great except in the appwidget.

If I need to select data in the appwidget onUpdate, then it's ok.

but when I try to do an UPDATE from the appwidget, I get the "database DATABASE_FLE already closeed" error.

What can it be?

I added some logs where I'm closing the db, and non of those lines execute before this error.. the db should be ok.

Any ideas?

Thanks.

Ran
  • 4,117
  • 4
  • 44
  • 70

2 Answers2

0

What i am thinking about, is case in which android may close/unload your activity (this may happend for example if there is a little memory left on the device), but in this time your appwidget is still active.

Now, because of your activity is closed, hence you db is closed. Appwidget tryes to update DB but its is already closed.

Check this scenario but anyway you should take this scenario in consideration. :)

Jviaches
  • 826
  • 3
  • 14
  • 30
  • in the appwidget onUpdate, I create a new database object and open the database.. the SELECT works.. I get the error when I try to update (actually when I call bind on an sql statement object) – Ran Apr 09 '12 at 17:56
0

The problem was that I compiled an SQLiteStatment object and saved it for future uses, while my database object got replaced.. so SQLiteStatement needs to re-compile again.

I had something like this:

public void func(SQLiteDatabase db) {
   if (mStatement == null)
      mStatement = db.compileStatement(SQL);

   mStatement.execute();

The problem with this is that the db object changed in the function call, and the first db object that compiled statement was already closed.

The solution was to remove the if-null, and to compile the statement for every db object.

Ran
  • 4,117
  • 4
  • 44
  • 70
  • I believe its very important to post solution to your problem (in case no one did it before) because others developers can learn from it. – Jviaches Apr 10 '12 at 07:36