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.
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 :)
Related
I was watching android tutorial on databases. They create a class and extend SQLiteOpenHelper. Then they would create a static final string for the database name. When you request a writable database it will always select using the static string. What is the convention used for selecting a different database? Just create another class and extend SQLiteOpenHelper? Also can you pass SQLiteDatabase from one activity to another using intent.putExtra or similar method?
What is the convention used for selecting a different database?
The SQLiteOpenHelper handles more than just a database name. It handles schema version, creation, upgrades, etc... So I would expect the convention to be to define another class and extend SQLiteOpenHelper.
Can you pass SQLiteDatabase from one activity to another using intent.putExtra or similar method?
No, you can't. But you can create a new object of the class describing the database (that one that extends SQLiteOpenHelper, and request a database from it. Internally the SQLiteOpenHelper keeps a single writable connection to a database.
How can i open a database in a class that is not a subclass of Activity?
In a subclass of Activity, i can use openOrCreateDatabase() but can i open a database in a different class?
I tried making the database instance a static one and open it in an Activity and get the static instance in the other class, but it throws an exception stating the database is closed.
Check out this tutorial.
I went through it and it's a really good tutorial on how to use SQLite in Android.
Essentially you need to create a database helper class that will do the table creation. Then you can use this helper class in your Activity to create your database and or tables.
It's common practice to use a SQLite Database Adapter and sometimes a helper class that is separate from the activity that is utilizing the database. Here is a link to a example that uses the code. THe vogella tutorial is also good but the use of a ContentProvider makes it a bit tough to understand what things need to be in there for a SQLite DB only.
Essentially, the helper class is responsible for creating, updating and deleting the DB while the adapter class handles the methods for changing values, deleting rows, and actually calling the helper to open the database.
Currntly i am creating SQLite database in android using following code:
SQLiteDatabase db;
try{
SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/bangla.rana.fahim/databases/dictionary", null,0);
dbe.close();
}
catch(SQLiteException e){
db = openOrCreateDatabase("dictionary", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS LIST(wlist varchar,ex varchar);");
db.close();
}
And retrieving data using cursor like:
Cursor cc = db.rawQuery(q, null);//q is the desired query.
But i have seen many example of using helper and adapters for the very same purpose. So, my question is what are the benefits of using them?
I guess you are referring to SQLiteOpenHelper, the documentation is pretty clear:
A helper class to manage database creation and version management.
You create a subclass implementing onCreate(SQLiteDatabase),
onUpgrade(SQLiteDatabase, int, int) and optionally
onOpen(SQLiteDatabase), and this class takes care of opening the
database if it exists, creating it if it does not, and upgrading it as
necessary. Transactions are used to make sure the database is always
in a sensible state.
With this you don't have to manually create your database and take care of upgrading it. You just have to implement the two methods onCreate() and onUpgrade() with your database logic and android will make sure to call this methods when is needed. I don't know at what are you referring when you say adapter.
Well it is more concerned about software desing patters. In the SQLiteOpenHelper you do the tasks more related with creating and updating the database when user installs the app or in a db version change.
In an Adapter class which holds the previous class you usually hold a reference to the db and you have the methods to actually interact with it doing the different kind of queries (selects, inserts...).
This way you have you code well encapsulated and therefore it is much easier to maintain.
Is it possible to use some kind of ddl script in SQLiteHelper to create database?
Yes. I usually have a main DB class in my application that encapsulates an inner class that derives from SQLiteOpenHelper. In the onCreate, you get a SQLiteDatabase instance as a parameter. Call db.execSQL(String) with the appropriate script(s) to create your tables and populate them.
Is it necessary to extends SQLiteOpenHelper.I just want to copy the database(read only) from aseets folder.
It is not necessary to extends SQLiteOpenHelper,To create and upgrade a database in your Android application you usually subclass "SQLiteOpenHelper". In this class you need to override the methods onCreate() to create the database and onUpgrade() to upgrade the database in case of changes in the database schema. Both methods receive an "SQLiteDatabase" object.
SQLiteOpenHelper provides the methods getReadableDatabase() and getWriteableDatabase() to get access to an "SQLiteDatabase" object which allows database access either in read or write mode.
For the primary key of the database you should always use the identifier "_id" as some of Android functions rely on this standard.
if you just want to copy the database then its fine.
But if you want to read,update,delete or create new record in database then there are two ways
Extend sqlitedatabase
Use ormlite wrapper
Hope this will help.
Yes, it not need ,if you just want copy database file and don`t operate database, it just a file I/O operate.