I have two different fields in my Android activity whose text values correspond to fields in the same DB table. However, only one field is able to be updated in the DB. The activity calls this updateUser function, seen below, two times (one for each field). The first field, token1, is successfully updated. The second field, token2, is not, even though the db.update() function returns 1 for both fields.
public class DatabaseHandler extends SQLiteOpenHelper
{
...
...
public int updateUser(String username, String domain, String columnName, String value)
{
ContentValues contentValues = new ContentValues();
contentValues.put(columnName,value);
SQLiteDatabase db = this.getWritableDatabase();
int rowsUpdated = db.update("table",contentValues,"username = ? AND domain = ?",new String[] {username,domain});
Log.d(TAG,rowsUpdated + " rows updated.");
db.close();
return rowsUpdated;
}
...
...
}
From the my activity class:
String token1 = editText_token1.getText().toString();
String token2 = editText_token2.getText().toString();
DatabaseHandler dh = new DatabaseHandler(this);
if (token1 != null && !token1 .equals("") && !token1 .equals("null"))
{
int rowsUpdated = dh.updateUser(user.getUsername(), user.getDomain(), "token1 ", token1 );
Log.d(TAG,"Number of rows updated: " + rowsUpdated);
}
if (token2 != null && !token2 .equals("") && !token2 .equals("null"))
{
int rowsUpdated = dh.updateUser(user.getUsername(), user.getDomain(), "token2", token2 );
Log.d(TAG,"Number of rows updated: " + rowsUpdated);
}
user = dh.getUserById(user.getId());
dh.close();
The two editText objects are editText fields from the layout, which the class is able to locate. The editText objects have their text content automatically populated with the database's content during the onCreate() function in this activity.
Using Log.d() statements, I am able to verify from both the DatabaseHandler and the activity classes that the second field, token2, is not being updated in the DB. Any ideas what is going on here?
Aucun commentaire:
Enregistrer un commentaire