开发者

Singleton - database connection to Sqlite

开发者 https://www.devze.com 2023-02-06 13:36 出处:网络
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.

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: Do开发者_如何学编程es the database handler class also need to be a Singleton? Thanks.


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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消