[英]Android - Check if row exists in table and update it
Unfortunately all the solutions I could find, would not help me with my problem.不幸的是,我能找到的所有解决方案都无法帮助我解决问题。
With a button click I want to add some value to my database table, but first it should check if the row already exists in my table, if so it just should update the row.通过单击按钮,我想向我的数据库表添加一些值,但首先它应该检查该行是否已经存在于我的表中,如果存在,它应该更新该行。
Here are my Codes:这是我的代码:
MAIN ACTIVITY: (Just the Button Click)主要活动:(只需单击按钮)
ingredient= new Ingredient();
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < dataSource.size();i++){
tvname = (TextView) shoppingList.getChildAt(i).findViewById(R.id.tvName);
tvamount = (TextView) shoppingList.getChildAt(i).findViewById(R.id.tvAmount);
String nameTV = tvname.getText().toString();
String amountTV = tvamount.getText().toString();
ingredient.setName(nameTV);
ingredient.setAmount(amountTV);
ingredient.setId(i);
TableIngredient.getInstance(IngredientShopping.this).checkRow(ingredient);
}
DATABASE TABLE:数据库表:
public class TableIncredient extends SQLiteOpenHelper {
public static TableIngredient INSTANCE = null;
public static final String DB_NAME = "INGREDIENT_TABLE";
public static final int VERSION = 1;
public static final String TABLE_NAME = "ingredient_table";
public static final String KEY_ID = "ID";
public static final String COL_NAME = "Name";
public static final String COL_AMOUNT = "AMOUNT";
public TableIngredient (Context context) {
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createQuery = "CREATE TABLE " + TABLE_NAME
+ "(" + KEY_ID + " INTEGER PRIMARY KEY, "
+ COL_NAME + " TEXT NOT NULL, "
+ COL_AMOUNT + " INTEGER DEFAULT NULL)";
db.execSQL(createQuery);
}
public boolean checkRow(Ingredient ingredient){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(TABLE_NAME,
new String[]{KEY_ID, COL_NAME, COL_AMOUUNT},
KEY_ID + " =? AND " + COL_NAME + " =? AND " + COL_AMOUNT + " =?" ,
new String[]{String.valueOf(ingredient)},
null, null, null, null);
ContentValues values = new ContentValues();
values.put(COL_NAME, ingredient.getName());
values.put(COL_AMOUNT, ingredient.getAmount());
if (c.moveToFirst()){
increseIngredient(ingredient);
return true;
}
else {
long newID = db.insert(TABLE_NAME, null, values);
db.close();
getIngredient(newID);
return false;
}
}
}
With this code it always uses the else statement and I do not know why this happens.使用这段代码,它总是使用 else 语句,我不知道为什么会发生这种情况。
I hope someone can help me to create a new row if it not exists and update the row if it exists.我希望有人可以帮助我创建一个不存在的新行,如果存在则更新该行。
With this code it always uses the else statement and I do not know why this happens.
使用这段代码,它总是使用 else 语句,我不知道为什么会发生这种情况。
The reason why this happens is that the 4th parameter to the query method is new String[]{String.valueOf(ingredient)}
发生这种情况的原因是查询方法的第 4 个参数是
new String[]{String.valueOf(ingredient)}
This will resolve to being a value something like Ingredient@1091 do you have an ingredient row that has an ID like that ( rhetorical as that cannot be the case due to the ID column being an alias of the rowid and therefore ONLY integer values can be stored ).这将解析为类似于Ingredient@1091的值,您是否有一个具有类似 ID 的成分行(修辞,因为 ID 列是 rowid 的别名,因此只能是整数值存储)。
The reason that Ingredient@1091 is that the String value of the Ingredient object will be a pointer to the object. Ingredient@1091的原因是 Ingredient 对象的 String 值将是一个指向该对象的指针。
As such no rows will ever be returned.因此,不会返回任何行。
If instead you used new String[]{String.valueOf(ingredient.getId),ingredient.getName,ingredient.getAmount)}
如果您改为使用
new String[]{String.valueOf(ingredient.getId),ingredient.getName,ingredient.getAmount)}
The 3 values would (should), be the correct values (assuming that the ID is stored correctly in the Ingredient).这 3 个值将(应该)是正确的值(假设 ID 正确存储在成分中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.