简体   繁体   中英

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.

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.

The reason why this happens is that the 4th parameter to the query method is 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 ).

The reason that Ingredient@1091 is that the String value of the Ingredient object will be a pointer to the object.

As such no rows will ever be returned.

If instead you used 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM