Search code examples
javaandroidsqlitecontent-values

android sql update not working


This is how the table and row look like :

public static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_NAME +
        "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_INSTANT +
        " TINYINT not null, " + COLUMN_CHANCE + " int not null);";

public static final String TABLE_INSERT = "INSERT INTO " + TABLE_NAME + " (" +
        COLUMN_ID+"," + COLUMN_INSTANT +","+ COLUMN_CHANCE
        +") VALUES (" +
        "1," + "0," + "10"
        +");";

And this is how I try to update them :

try {
        open();
        ContentValues values = new ContentValues();
        values.put(column_name, value);
        database.beginTransaction();
        database.update(MySQLiteHelper.TABLE_NAME, values, MySQLiteHelper.COLUMN_ID + " = " + whereClause,null);
        database.setTransactionSuccessful();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        database.endTransaction();
    }
    close();

The previous code would be equal to this query :

UPDATE options SET instant = 0 WHERE _id = 1

But the change is never applied. I check the table before and after the update but the values are the same, can anyone suggest anything?


Solution

  • You can make method to update...here is an example, change column name accordingly.

    public int updateDetail(String new_name,String surname) {
            ContentValues values = new ContentValues();
            values.put("name", new_name);
            return Db.update(TABLE_NAME ,values, SURNAME +"=?",new String[]{surname});
    }