对于了解如何更新数据库行的更一般帮助,这次文档实际上很有帮助:
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);
这个页面也很有帮助:Working with SQLite Database (CRUD operations) in Android
就我而言,我做了一个这样的方法:
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;
}