I am trying to find the most efficient logic for the next situation.
I want to implement on my Android app storing in sql most used functions/actions by users.
dbase structure:
| _id | act_name (text unique) | counter (integer) |
code: (src https://stackoverflow.com/a/20568176/8006698)
int rowID = (int) db.insertWithOnConflict(TABLE_NAME, null, cv,SQLiteDatabase.CONFLICT_IGNORE);
if (rowID != -1){
myLog("Written to base with id = " + rowID);
} else {
db.execSQL("UPDATE stats SET counter=counter + 1 WHERE act_name = '" + mAction+"'");
myLog("stats updated");
}
I was surprised, that 'execSQL' doesn't return value as 'update' method. I would use:
int rowID = db.update(...);
if (rowID == 0) {
//if not updated then insert
long insertedID = db.insert(...);
myLog("Inserted to base ");
} else
myLog("base updated");
but can't increase here count directly as 'counter=counter + 1' so I've to use 'insertWithOnConflict'. I'm not sure, is it good practice to use it. Waiting for your ideas.