![](/img/trans.png)
[英]How to get the value of a specific table field by knowing the id and the column?(SQLite)
[英]How to update a specific field in SQLite with key id?
我需要使用他的 ID 將我的 SQLite 數據庫中的字段 isActive 更新為特定用戶,如果該字段等於 1,他是活動的,而不是活動字段等於 0。我想從 1(活動)更新到 0(不活動)。 cv = ContentValue,db = SQLiteDatabase,hlp = HelperDB。
cv.put(Student.isActive, 0);
db = hlp.getWritableDatabase();
db.update(TABLE_STUDENTS, cv, key, null);
db.close();
最好的解決辦法是......
public void isActive(int id, boolean isActive)
{
int is_active = (isActive) ? 1 : 0;
SQLiteDatabase db = this.getWritableDatabase();
try {
ContentValues cv = new ContentValues();
cv.put(IS_ACTIVE, is_active);
// The columns for the WHERE clause
String selection = (ID+ " = ?");
// The values for the WHERE clause
String[] selectionArgs = {id};
db.update(TABLE_NAME, cv, selection, selectionArgs);
}
catch (Exception e) {
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.