There are some approaches that may result in good practice.
Vogella's blog was always a good reference to me. Have a look to this approach. It's made out of a class which controls the creation and update of the database and another one as a DAO to make the necessary request for a certain type of objects.
Personally I've always used a combination of both, together within a Singleton class, which was perfect for maintaining and controlling the whole lifecycle of the instance (in my case SQLiteDatabase) which allows you to make the tasks you need to perform without opening, closing or losing reference to your database object. The same class is inheriting from SQLiteOpenHelper to manage the creation and update of the database.
A general structure could look like this:
package your.package;
import java.io.File;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBManager extends SQLiteOpenHelper {
private static DBManager sInstance;
private static SQLiteDatabase db;
private final String LOG_TAG = this.getClass().getSimpleName();
private static final String DB_NAME = "DBName";
private static final int DB_VERSION = 1;
public static synchronized DBManager getInstance(Context _c) {
if (sInstance == null) {
sInstance = new DBManager(_c);
}
return sInstance;
}
private DBManager(Context _c) {
super(_c, DB_NAME, null, DB_VERSION);
}
public void deleteTable(String table) {
if (db == null || !db.isOpen()) {
db = getWritableDatabase();
}
db.execSQL("DROP TABLE " + table);
db.close();
}
public void reset() {
File dbFile = new File(getReadableDatabase().getPath());
dbFile.delete();
sInstance = null;
db.close();
}
public static void closeDB() {
if (sInstance != null && db != null && db.isOpen()) {
db.close();
}
}
public long insertContent(String table, ContentValues cv) {
if (db == null || !db.isOpen()) {
db = getWritableDatabase();
}
return db.insert(table, null, cv);
}
public void updateContent(String table, ContentValues cv, String whereClause, String[] whereArgs) {
if (db == null || !db.isOpen()) {
db = getWritableDatabase();
}
db.update(table, cv, whereClause, whereArgs);
}
@Override
public void onCreate(SQLiteDatabase db) {
if (db == null || !db.isOpen()) {
db = getWritableDatabase();
}
db.execSQL("your query");
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.i(LOG_TAG, "onUpgrade old: " + oldVersion + " new: " + newVersion);
}
}