我尝试阻止用户传入空输入。
private Uri insert(Uri uri, ContentValues contentValues) {
if (contentValues.getAsString(StudentEntry.COLUMN_NAME) == null) {
throw new IllegalArgumentException("A name is required");
}
if (contentValues.getAsString(StudentEntry.COLUMN_CITY) == null) {
throw new IllegalArgumentException("A city is required");
}
//Perform insert operation here.
}
无效数据仍然可以进入数据库。
答案 0 :(得分:0)
尝试以下验证:
if(!contentValues.containsKey(StudentEntry.COLUMN_NAME) || TextUtils.isEmpty(contentValues.getAsString(StudentEntry.COLUMN_NAME))) {
throw new IllegalArgumentException("A name is required");
}