Reputation: 297
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?
Upvotes: 4
Views: 40209
Reputation: 627
Try by this one :
db.update("yourtable", cvalue, "Profile_Name_for_weekly="+"'"+profilename+"'", null);
Upvotes: 0
Reputation: 14710
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});
Upvotes: 14
Reputation: 511626
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;
}
Upvotes: 2