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
SQLiteOpenHelper
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).