0

I have database updates like the two below, throughout my code. Most of my updates open the database first before updating the record and then closes the database after the record has been updated. I have noticed that not using this statement: mDb = Helper.getWritableDatabase(); before the insertion and this statement: mDb.close(); after insertion, will cause a force close error sometimes, but not always. What is the proper way. Do I use the open and close statements all the time or only when I have to or should I always open and then close during the update process. What is the proper technique. Here is the snippet with the open close statements. Thanks in advance. Is the open statement necessary?

            // Open connections to the database
            mDb = Helper.getWritableDatabase();             

            // update 1
            String strFilter7 = "_id=" + 7;
            ContentValues args7 = new ContentValues();
            args7.put(COL_VALUE, newB1ftgvalue);
            mDb.update("VarData", args7, strFilter7, null);

             // update 2
            String strFilter11 = "_id=" + 11;
            ContentValues args11 = new ContentValues();
            args11.put(COL_VALUE, newB2ftgvalue);
            mDb.update("VarData", args11, strFilter11, null);

            // closes database
            mDb.close();    
lastshadowrider
  • 103
  • 1
  • 9

3 Answers3

2

It is good practice to always call close() after you are done with database updates. If you haven't closed the database you may see errors. Once database is open, you may do multiple updates. It shouldn't be an issue. One thing to take care is, it is better not to keep open connection for long time due to lot of reasons. Here is good discussion on this topic.

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
2

Do not close it and do only have one sqlite helper. It's basically a static openhelper. There is no problem with never closing your database. This link gives a good piece of code that works great. You will not have memory leaks with an open database. You will however have problems with open cursors, so make sure to close those.

http://www.touchlab.co/blog/single-sqlite-connection/

A good discussion is here: What are the best practices for SQLite on Android?

I realized the link I posted has changed since when I once first viewed it. Change:

instance = new DatabaseHelper(context);

to

instance = new DatabaseHelper(context.getApplicationContext());

and

public class DatabaseHelper extends OrmLiteSqliteOpenHelper

to

public class DatabaseHelper extends SqliteOpenHelper
Community
  • 1
  • 1
Kyle
  • 611
  • 4
  • 6
  • Not closing closable things is bad practice even if it does not make problems (yet). – zapl Apr 18 '12 at 22:11
  • Thanks for the link, helped a lot! – Caumons Apr 18 '12 at 22:16
  • 1
    @zapl The problem is... How do we know when the db should be closed? I think there is no way in Android... :( And for the Context I would use the application Context. – Caumons Apr 18 '12 at 22:18
  • you can do open - [query|update|insert|delete]* - close each time you need to access the database. – zapl Apr 18 '12 at 22:24
  • @Caumons In the link I gave, it calls the application context inside the code, so the application context is always used. – Kyle Apr 18 '12 at 22:24
  • @zapl Coders who know more than me have stated that it does not cause problems. I do not know enough to refute them. See the discussion at the 2nd link I provided. – Kyle Apr 18 '12 at 22:25
1

You need to open the db as a writable database in order to modify its data, so yes, you have to open it before updating records.

Caumons
  • 9,341
  • 14
  • 68
  • 82