0

In my DataBase Adapter I have a list of methods that puts data, upgrades data and retrieves data. In each method I Instatiate like this

SQLiteDatabase db = this.getWritableDatabase();

I used to close the database on every method and my app used to crash. Then I left them open in every method so the crashing stopped.

Does the line of code open several database connections when I use several methods with the same line?

Is there a better approach to open the database?

Here's one of my methods

public int getAntalRows() throws Exception {
    SQLiteDatabase db = this.getWritableDatabase();


    Cursor mCount= db.rawQuery("SELECT COUNT (*) FROM " + TABLE_PRODUCTS, 
    null);
    mCount.moveToFirst();
    int x= mCount.getInt(0);
    mCount.close();
    return x;

}
Waffles.Inc
  • 3,310
  • 4
  • 13
  • 17
  • I think u can try database connection pooling api,check this link http://stackoverflow.com/questions/7444157/sqlite-connection-pool – hayat Jan 20 '16 at 05:06
  • You had som good links there, I need to read those things, maybe I find what I need. – Waffles.Inc Jan 20 '16 at 05:09
  • But my first question. Does this open several connections every time one of my methods is called? – Waffles.Inc Jan 20 '16 at 05:10
  • Yes absolutely. Several connections would be open. Another approach is to have a connection specific to the class, commit only from write methods, and close in the destructor. You'll have to maintain state i.e. open a new connection afresh if the one available is stale and doesn't do what you want. – Spade Jan 20 '16 at 05:17

0 Answers0