Update method returns true. But when listing the whole table, new rows were created for each update.
ONE table, three columns: PRIMARY_ID, MY_ID, MY_ANOTHER_ID.
I have the following methods:
public void insertOrUpdate(String lStringId){
boolean present= search(lStringId);
if(present){
updateData(lStringId);
} else{
insertData(lStringId);
}
}
Search:
private boolean search(String lStringId) {
Cursor cursor = null;
try {
cursor = mDatabase.query(TABLE_NAME, null,
MY_ID + " = ?", new String[]{lStringId}, null, null, null);
if (null != cursor && cursor.getCount() == 1 && cursor.moveToFirst()) {
return true
}
Log.d(TAG, "-search, cursor.getCount() = " + cursor.getCount());
return false;
} catch (Exception e) {
Log.e(TAG, "-search, Error: "+ e.getLocalizedMessage());
return false;
} finally {
if (null != cursor) {
cursor.close();
cursor = null;
}
}
}
Update:
public boolean updateData(String lStringId) {
Log.d(TAG, " -updateData");
ContentValues lValues = new ContentValues();
lValues.put(MY_ANOTHER_ID, lStringId+"extra");
if (mDatabase.update(TABLE_NAME,
lValues, MY_ID + "= '" + lStringId + "'", null) > 0) {
Log.d(TAG, " -updateData, true");
return true;
}
return false;
}
Insert:
public long insertData(String lStringId) {
Log.d(TAG, " -insertData");
ContentValues dataValues = new ContentValues();
dataValues.put(MY_ID, lStringId);
long result = mDatabase.insert(TABLE_NAME, null, dataValues);
Log.d(TAG, "result: " + result);
return result;
}
After the first new insert, the next time I call insertOrUpdate method, search method returns true. And updateData method was called and it also returns true.
-search, cursor.getCount() = also prints 1.
But when I check the contents of the table, there are two rows with same MY_ID value and different MY_ANOTHER_ID values.
What am I missing?
Aucun commentaire:
Enregistrer un commentaire