MYaser
MYaser

Reputation: 379

(SQLiteDatabase & SQLiteOpenHelper) Duplicate functionality

I can open a SQLiteDatabase in two ways:

SQLiteDatabase.openDatabase()
SQLiteOpenHelper.getWritableDatabase ()

It seems that there is no difference between two approaches except more control when using SQLiteDatabase.openDatabase()

Q1- why duplication?

Q2- how can i benefit from that from the perspective of Software Design

Upvotes: 0

Views: 868

Answers (2)

Nikolay Elenkov
Nikolay Elenkov

Reputation: 52936

SQLiteDatabase offers lower level access and control, while, as mentioned above, SQLiteOpenHelper takes care of a lot of boilerplate code, and you should generally prefer it. One situation where you need to use SQLiteDatabase is when you want to open a database (often pre-populated) on external storage -- with SQLiteOpenHelper you cannot specify the DB file location, it defaults to creating one in your app's private directory (under org.myapp/databases).

Upvotes: 0

Squonk
Squonk

Reputation: 48871

Creating your own class which extends SQLiteOpenHelper allows you to remove a lot of SQLite db code from your main code. You override the onCreate(...) and onUpdate(...) methods to allow it to automatically create the database when your app is first run and to update the database in future upgrades of your app.

It's also useful as you can add methods to your extended SQLiteOpenHelper to do all of the 'boiler-plate' tasks (queries, insertions, deletions etc). Effectively, your main code never needs a reference to a database - it can simply call methods on your extended SQLiteOpenHelper class.

Upvotes: 2

Related Questions