Question: Update values of a specific column of a specific row. Android SQLite

Question

Update values of a specific column of a specific row. Android SQLite

Answers 2
Added at 2017-08-20 06:08
Tags
Question

I have a recyclerview cardholder that inflates using the values of 'NAME' from the table 'ORDERTABLE'. The cardholder also have an EditText which displays the values of column 'QUANTITY' in my SQLite database.

I also have a button to update the database for every changes in EditText.

I have this table ORDERTABLE

id      NAME         QUANTITY

 1      Order1          1
 2      Order2          1
 3      Order3          1
 4      Order4          1 

Being more specific, how can i update the QUANTITY of Order2 on onButtonPressed with the new values of my EditText.

EDIT...

I have tried this code but nothing happened

Button to update values

public void addButtonClick(TextView tv_cardrowName_atc, TextView tv_currentPrice_atc, EditText et_quantity_atc, int position) {

            int thisQuantity = Integer.parseInt(et_quantity_atc.getText().toString());
            thisQuantity++;

            String orderName = tv_cardrowName_atc.getText().toString();
            String oldQuantity = et_quantity_atc.getText().toString();

            String newQuantity = String.valueOf(thisQuantity);
            sqliteCBLCAdapter.selectUpdateItem(orderName, oldQuantity, newQuantity);


            et_quantity_atc.setText(String.valueOf(thisQuantity));
        }

Update Method

public String selectUpdateItem(String orderName, String oldQuantity, String newQuantity) {

    SQLiteDatabase sqLiteDatabase = sqLiteCBLC.getWritableDatabase();
    String[] columns = {SQLiteCBLC.COL_ORDNAME, SQLiteCBLC.COL_QUANTITY};
    Cursor cursor = sqLiteDatabase.query(SQLiteCBLC.TABLE_NAME, columns, SQLiteCBLC.COL_ORDNAME + " = '" + orderName + "'", null, null, null, null);
    StringBuffer stringBuffer = new StringBuffer();
    while (cursor.moveToNext()) {
        int index1 = cursor.getColumnIndex(SQLiteCBLC.COL_ORDNAME);
        int index2 = cursor.getColumnIndex(SQLiteCBLC.COL_QUANTITY);
        String order = cursor.getString(index1);
        String quantity = cursor.getString(index2);

        ContentValues contentValues = new ContentValues();
        contentValues.put(SQLiteCBLC.COL_QUANTITY, newQuantity);
        String[] whereArgs = {quantity};
        sqLiteDatabase.update(SQLiteCBLC.TABLE_NAME, contentValues, SQLiteCBLC.COL_QUANTITY + " =? ", whereArgs);

        stringBuffer.append(order);

    }



    return stringBuffer.toString();
}
Answers to

Update values of a specific column of a specific row. Android SQLite

nr: #1 dodano: 2017-08-20 06:08

You can try like this below code, In your case you while updating you are updating based on quantity, multiple order will have the same quantity. just check the order name and update it.

public void selectUpdateItem(String orderName, String oldQuantity, String newQuantity) {

    if (TextUtils.isEmpty(order)) {
        return;
    }

    ContentValues contentValues = new ContentValues();

    final String whereClause = SQLiteCBLC.COL_ORDNAME + " =?";
    final String[] whereArgs = {
            orderName
    }; 

    // if you want to update with respect of quantity too. try this where and whereArgs below

    //final String whereClause = SQLiteCBLC.COL_ORDNAME + " =? AND " + SQLiteCBLC.COL_QUANTITY + " =?";
    //final String[] whereArgs = {
            //orderName, String.valueOf(oldQuantity)
    //};

    contentValues.put(SQLiteCBLC.COL_QUANTITY, newQuantity);

    SQLiteDatabase sqLiteDatabase = sqLiteCBLC.getWritableDatabase();
    sqLiteDatabase.update(SQLiteCBLC.TABLE_NAME, contentValues,
            whereClause, whereArgs);
}
nr: #2 dodano: 2017-08-20 07:08

The easiest way for you to achieve this would be to use a SQL update query as follows:

From the SQLite Web Site:

The SQLite UPDATE Query is used to modify the existing records in a table. You can use a WHERE clause with UPDATE query to update selected rows, otherwise all the rows would be updated.

The syntax for the update query is as follows:

 UPDATE table_name
 SET column1 = value1, column2 = value2...., columnN = valueN
 WHERE [condition];

So in your case your sql update query would look some thing like this:

UPDATE ORDERTABLE SET QUANTITY = (INSERT VALUE OF YOUR EDIT TEXT) WHERE NAME =   'Order2'

You can then execute your query by using the execSQL() method of your SQLiteDatabase object that you have and passing in the sql query above as the string parameter.

Source Show
◀ Wstecz