1

I have a singleton class that creates a connection to a Sqlite db and runs queries. I need to pull the database stuff out of the Singleton and create a database handler class. My question is: Does the database handler class also need to be a Singleton? Thanks.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Tim OMalley
  • 423
  • 6
  • 14

2 Answers2

0

Probably not, you could have a class that is instantiated normally every time and used like every other class, I do not write singleton data layers since long time and not even static classes for it any more.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
-3

Actually you don't need to write your own singleton. You just need to have a class which inherits from SQLiteOpenHelper

Later in the code you just need to use: SQLiteDatabase db = helper.getWritableDatabase();

The SQLiteOpenHelper cares if the D already exists. If yes, it gives to the DB as readable or writeable database. If no DB is available it creates one... like this you don't have to check if the DB is already created.

You can see here a full example i posted some days ago...
Android - Sqlite database method undefined fot type

Hope this helps :)

Community
  • 1
  • 1
Beasly
  • 1,517
  • 4
  • 20
  • 30