如何使用 edittexts 更新 Android 中 sqlite 行的可变列数
How to update a variable number of columns of a sqlite row in Android with edittexts
如果我使用下面的代码
public boolean updateContact(Integer id, String name, String surname, byte[] image)throws SQLiteException
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("MyName", name);
contentValues.put("MySurname", surname);
contentValues.put("Photo", image);
db.update("details", contentValues, "_id = ? ", new String[] { Integer.toString(id) } );
return true;
}
然后,如果我忽略添加一列,它会保存空值,我如何才能仅使用那些不为空的字段创建更新?
是否可以添加
db.update("details", contentValues, "_id = ? OR name=? OR surname=? ", new String[] { Integer.toString(id) } );
谢谢
有没有什么变量是一直设置的,比如id?如果是这样,您只需在将变量名称和姓氏添加到 contentValues:
之前检查变量名称和姓氏是否为 null
if (name != null && !name.isEmpty()) {
contentValues.put("MyName", name);
}
如果我使用下面的代码
public boolean updateContact(Integer id, String name, String surname, byte[] image)throws SQLiteException
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("MyName", name);
contentValues.put("MySurname", surname);
contentValues.put("Photo", image);
db.update("details", contentValues, "_id = ? ", new String[] { Integer.toString(id) } );
return true;
}
然后,如果我忽略添加一列,它会保存空值,我如何才能仅使用那些不为空的字段创建更新?
是否可以添加
db.update("details", contentValues, "_id = ? OR name=? OR surname=? ", new String[] { Integer.toString(id) } );
谢谢
有没有什么变量是一直设置的,比如id?如果是这样,您只需在将变量名称和姓氏添加到 contentValues:
之前检查变量名称和姓氏是否为 nullif (name != null && !name.isEmpty()) {
contentValues.put("MyName", name);
}