Logging insertion to Database on Android
I'm currently needing to log the insertions/updates/deletes from my
database but i don't get what i can call a great result.
I've just implemented SQLiteDatabase.CursorFactory but that is just for
queries, and not data modification.
What i've done so far is to simply log what i am inserting calling a
function that receives the parameters and the table ta it will insert.
I have a general function inside SQLiteOpenHelper to insert:
public void insertToTable(ContentValues cv, String table){
status = dBase.insert(table,null,cv);
if(status != -1){
logInsert(cv,table);
}
}
public void logInsert(ContentValues cv, String tabela) {
StringBuilder raw = new StringBuilder();
Set<String> keys = cv.keySet();
String[] columns = keys.toArray(new String[keys.size()]);
int i;
raw.append("INSERT INTO " + tabela + "(");
for (i = 0; i < columns.length-1; i++) {
raw.append(columns[i] + ",");
}
raw.append(columns[i] + ") VALUES(");
for (i = 0; i < cv.size()-1; i++) {
raw.append("'" + cv.get(columns[i]) + "',");
}
raw.append("'" + cv.get(columns[i]) + "');");
Log.v("SQL",raw.toString());
}
This is working pretty well but don't think is the best way to do as i am
iterating over the same ContentValues three times (one time to insert and
other two just for logging).
What can i do to improve it? There's a good way to fit this also to
update/delete ?
Thanks in advance!
No comments:
Post a Comment