-1

Here is my SQLite method:

   public int updateSingleRow(int id, int i){
    SQLiteDatabase db = myDBHelper.getReadableDatabase();
    ContentValues values = new ContentValues();
    values.put("fab", i);
    String[] whereArgs = {String.valueOf(id)};

    int count = db.update(MyDBHelper.COIN_DETAILS_TABLE ,values,MyDBHelper.UID + "=?",whereArgs);
    CoinDetail cd = getDataByID(id);
    Log.d("CCP",""+cd.getFab());
    return count;
}

Here is my table definition where I have defined table columns and i want to update fab column:-

  String query_coinDetailTable = "CREATE TABLE " + COIN_DETAILS_TABLE + "("+ UID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + CAP_24HR_CAHNGE + " TEXT," + LONG + " TEXT," + MK_CAP + " TEXT," + PERC + " TEXT," + PRICE + " TEXT,"
                + SHAPESHIFT + " TEXT," + SHORT + " TEXT," + SUPPLY + " TEXT," + USD_VOLUME + " TEXT," + VOLUME + " TEXT,"
                + VWAP_DATA + " TEXT," + VWAP_DATA_BTC + " TEXT," + FAB + " INTEGER DEFAULT 0" + ")";
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    just put your code in try catch block and update your question with logcat. – Jaydip Kalkani Jan 25 '18 at 08:40
  • 2
    After you run `updateSingleRow()`what is the value of `count`? – Sam Jan 25 '18 at 08:40
  • try also writing the quesy manually, and executing it. – Vladyslav Matviienko Jan 25 '18 at 08:43
  • i have printed the result in logcat it is returning 0 @Sam – Bhringesh Biswas Jan 25 '18 at 08:47
  • If `count` is 0 then check your `id` to make sure it matches the intended row in your database. – Sam Jan 25 '18 at 08:52
  • temporarilly add `Cursor csr = db.query(MyDBHelper.COIN_DETAILS_TABLE,null,MyDBHelper.UID + "=?",whereargs,null,null,null);` after the line `String[] whereargs.....` line. Then add line `Log.d("CCP","Number of rows to be updated will be " + String.valueOf(csr.getCount());` and then add line `csr.close();` If rows to be updated is 0 *(should be 0 or 1)* then the liklihood is that the id that you are passing is not that value of an id in the table. – MikeT Jan 25 '18 at 08:52

2 Answers2

1

The likelihood is that you are passing a value (id), as the first parameter to the updateSingleRow method, that does not match a UID column in one of the rows of the table.

You could ascertain this by temporarily amending the method to be :-

public int updateSingleRow(int id, int i){
    SQLiteDatabase db = DatabaseHelper.getReadableDatabase();
    ContentValues values = new ContentValues();
    values.put("fab", i);
    String[] whereArgs = {String.valueOf(id)};
    Cursor csr = db.query(MyDBHelper.COIN_DETAILS_TABLE,
            null,null,null,null,null,null);
    // Possible cause empty table
    if (csr.getCount() < 1) {
        Log.d("CCP","Table " + MyDBHelper.COIN_DETAILS_TABLE + " is empty.");
        return -1;
    }
    Log.d("CCP","Table " +  MyDBHelper.COIN_DETAILS_TABLE +
            " contains " + String.valueOf(csr.getCount() + " rows."));
    boolean foundflag = false;
    StringBuilder idlist = new StringBuilder("Other UID's found :- ");
    while(csr.moveToNext()) {
        int this_id = csr.getInt(csr.getColumnIndex(MyDBHelper.UID));
        if (this_id == id) {
            Log.d("CCP"," Matching row found for UID " + String.valueOf(this_id));
            foundflag = true;
            break;
        } else {
            idlist = idlist.append(" " + String.valueOf(this_id));
        }
    }
    csr.close();
    if (!foundflag) {
        Log.d("CCP","Search for UID " + String.valueOf(id) +
                " failed. " + idlist);
        return -1;
    }

    int count = db.update(MyDBHelper.COIN_DETAILS_TABLE ,values,MyDBHelper.UID + "=?",whereArgs);
    CoinDetail cd = getDataByID(id);
    Log.d("CCP",""+cd.getFab());
    return count;
}

This will tell you one of the following

  • (a) if the table is empty (a possible cause),
  • (b) if the passed id does not match, followed by a list of the UID's in the table or
  • (c) if a UID column matches the passed id.

It will also return -1 if any of the checks indicate that the update would not be done (empty table, no matching UID).

In brief it is unlikely that the issue is not a or b. If the issue is indicated as being a or b then you need to investigate as to why there are no rows in the table (a) or why the id that is passed does not match (b).

MikeT
  • 51,415
  • 16
  • 49
  • 68
0

try like this

updateContact()
    // Updating single contact
public int updateContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName());
    values.put(KEY_PH_NO, contact.getPhoneNumber());

    // updating row
    return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.getID()) });
}
Adil
  • 812
  • 1
  • 9
  • 29
  • I know how to work with updation all I need to know how to update the value in that particular column of my tabe kindly edit my code and post it...thanks – Bhringesh Biswas Jan 25 '18 at 09:26