Sqlite database updating a row android

40,069

Solution 1

You need to escape profilename. So you either add the single ' character:

db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY, values,Profile_Name_for_weekly +" = '"+ profilename + "'", null);

Or, the option I would follow:

db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY, values,Profile_Name_for_weekly +" = ?", new String[] {profilename});

Solution 2

For more general help in understanding how to update a database row, the documentation was actually helpful this time:

SQLiteDatabase db = mDbHelper.getReadableDatabase();

// New value for one column
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_TITLE, title);

// Which row to update, based on the ID
String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
String[] selectionArgs = { String.valueOf(rowId) };

int count = db.update(
    FeedReaderDbHelper.FeedEntry.TABLE_NAME,
    values,
    selection,
    selectionArgs);

This page was also helpful: Working with SQLite Database (CRUD operations) in Android

In my case I made a method like this:

public long updateTime(long rowId) {

    // get current Unix epoc time in milliseconds
    long date = System.currentTimeMillis();

    SQLiteDatabase db = helper.getWritableDatabase(); // helper is MyDatabaseHelper, a subclass database control class in which this updateTime method is resides
    ContentValues contentValues = new ContentValues();
    contentValues.put(MyDatabaseHelper.DATE_TIME, date); // (column name, new row value)
    String selection = MyDatabaseHelper.ID + " LIKE ?"; // where ID column = rowId (that is, selectionArgs)
    String[] selectionArgs = { String.valueOf(rowId) };

    long id = db.update(MyDatabaseHelper.FAVORITE_TABLE_NAME, contentValues, selection,
            selectionArgs);
    db.close();
    return id;
}
Share:
40,069
Dave
Author by

Dave

Updated on July 19, 2022

Comments

  • Dave
    Dave almost 2 years

    I am developing an android app, in which I need update a column in a table based on the a certain where clause.Here is the code below,

    public void updatethekeyofweeklycolumn(String profilename, String keystemp) 
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
    
        values.put(Profile_keysofweekly, keystemp);
    
        db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY, values,Profile_Name_for_weekly +" = "+profilename, null);
    }
    

    The above code is working fine with where clause as null, but its throwing a force close with the whereclause is set. Is my Query wrong?