dimanche 26 juillet 2015

SQLite Update / Replace Table Android

I have an app that gets JSON data from a server. I then put the parsed data into the android SQLite database and use the data as needed. This all works great, however, I am unable to find a method to update the whole table. The scenario would be that this Json Data feed gets updated every week on the server. I have two Questions:

What am I missing or what is the method for updating the SQLite table? (currently this just duplicates the data)

public void updateTable(Product product){
    SQLiteDatabase db = this.getWritableDatabase();
    try{
        ContentValues values = new ContentValues();
    values.put(KEY_TYPE_NAME, product.getmProductTypeName());
    // more columns here...
    db.update(TABLE_NAME, values, null,null);
    db.close();
    }catch(Exception e){
    Log.e("error:",e + "in updateData method")
    }

What is an ideal system for updating the data? Would it be silly and bad practice to just call the method when connected to internet?

Related Code in "Main Activity":

    handler = new DBHandler(this);
    NetworkUtils utils = new NetworkUtils(MainActivity.this);
    if (handler.getProductCount() == 0 && utils.isConnectingToInternet()) {
        new JsonDataParse().execute();
    }`

Related Code "DBhandler" Activity:

    public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DROP_TABLE);
    onCreate(db);
}

Aucun commentaire:

Enregistrer un commentaire