开发者

Why use SQLiteOpenHelper over SQLiteDatabase?

开发者 https://www.devze.com 2023-03-28 14:14 出处:网络
In my activity I have for example SQLiteDatabase db = openOrCreateDatabase(Preferences.DB_NAME, Context.MODE_PRIVATE, null);

In my activity I have for example

SQLiteDatabase db = openOrCreateDatabase(Preferences.DB_NAME, Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS data (id开发者_如何学编程 INTEGER PRIMARY KEY, value VARCHAR)");
Cursor dbResult = db.rawQuery("SELECT value FROM data", null);
// do sometning with cursors
dbResult.close();
db.close();

What's the benefit of using SQLiteOpenHelper like

DatabaseHelper helper = new DatabaseHelper(this);
SQLiteDatabase db = helper.getWriteableDatabase();
SQLiteDatabase db_2 = helper.getReadableDatabase();
Cursor dbResult = db_2.rawQuery("SELECT value FROM data", null);
// do sometning with cursors
dbResult.close();
helper.close();

Class itself

public class DatabaseHelper extends SQLiteOpenHelper {
    public DatabaseHelper(Context context) {
        super(context, Preferences.DB_NAME, null, Preferences.DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY, value VARCHAR)";
        db.execSQL(query);
        db.close();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}


SQLiteDatabase

SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.

SQLiteOpenHelper

A helper class to manage database creation and version management.

I will say this much, the onUpgrade that comes with SQLiteOpenHelper comes in REALLY handy when upgrading your application. It's mainly for creation and upgrading / version management. SQLiteDatabase is mainly for CRUD operations (you can create with it but that is what SQLiteOpenHelper is for).


SQLiteOpenHelper provides utilities to simplify the tasks of creating and initialising the database if it's not already created and converting the contents of the database when your application is upgrading and the database schema changes.

If you have a very simple database schema, then it doesn't really get you much, but for anything complicated it's a definite help. It makes sure that all the fiddly edge conditions are covered so that you don't have to, such as putting transactions in all the right places to avoid database corruption.


Above of other answers, one very important feature in SQLiteOpenHelper class, it has 2 synchronized methods, getWritableDatabase() and getReadableDatabase().

That means your database operations are thread safe.

Code snippet from SQLiteOpenHelper class

public SQLiteDatabase getReadableDatabase() {
    synchronized (this) {
        return getDatabaseLocked(false);
    }
}

and

public SQLiteDatabase getWritableDatabase() {
    synchronized (this) {
        return getDatabaseLocked(true);
    }
}


As it is written in the official developer reference

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.

This class makes it easy for ContentProvider implementations to defer opening and upgrading the database until first use, to avoid blocking application startup with long-running database upgrades.

For an example, see the NotePadProvider class in the NotePad sample application, in the samples/ directory of the SDK.

So SQLiteOpenHelper make things pretty easier.

You can use this link to learn how to handle SQLite with SQLiteOpenHelper. SQLiteOpenHelper Tutorial.

0

精彩评论

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