Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite usage from activity and service

Tags:

android

sqlite

I have created a databaseprovider class which uses single instance of db object. Object is created in main activity and closed onDestroy method. This seems ok (but get some errors such as: db already closed or db is not open on some users devices that I cannot simulate).

I want to add a service to the application for the content download and this service can run with scheduler which make me think about single instance of db object will not work. Should I use another object for the service, will it result consistency problems? Can you kindly advice what would be the best way?

Databaseprovider class exm:

public class DatabaseProvider {
private static DatabaseHelper helperWriter;
public static SQLiteDatabase db_global;
public DatabaseProvider(Context c) {
    helperWriter = DatabaseHelper.getHelper(c, true);
}

private static SQLiteDatabase getDB() {     

    if(db_global == null)           
        db_global = helperWriter.getWritableDatabase();
    else if(!db_global.isOpen()) {
        try {
            db_global.close();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        db_global = helperWriter.getWritableDatabase();
    }

    return db_global;
}

public String GetVersion() {
    SQLiteDatabase db = getDB();
    Cursor c = db.query(DatabaseHelper.PARAMETER_TABLE_NAME, new String[] {"VALUE"}, "KEY='Version'", null, null,null,null);
    String version = "";
    if(c.moveToNext())
    {
        version = c.getString(0);           
    }
    else
        version = "0";
    c.close();

    return version;
}

public long UpdateVersion(String value) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(DatabaseHelper.PARAMETER_COLUMN_VALUE, value);
    SQLiteDatabase db = getDB();   
    long r = db.update(DatabaseHelper.PARAMETER_TABLE_NAME, initialValues, "KEY='Version'", null);
    if(r <= 0)
        r = helperWriter.AddParameter(db, "Version", value);
    //db.close();   
    return r;
}   

public void CloseDB() {
    if (db_global != null)
        db_global.close();
    db_global = null;
    helperWriter.close();
}
 }

like image 265
tjay Avatar asked Sep 20 '12 11:09

tjay


1 Answers

Not sure if this will help, but...

you can't rely on onDestroy() in case the app crashes. Android may also keep your app in RAM, even if you exit it. Also, your main activity may get destroyed while the app is getting used if you are on a subactivity. It can also get recreated.

Sometimes it's better to have calls that open the DB, does stuff to it, and then closes it within the same function. If you are using a service, it may actually help things. I also am not sure if you should have a situation where a DB can be opened and/or accessed from a variety to different places at once without some management code

like image 106
Joe Plante Avatar answered Oct 31 '22 10:10

Joe Plante