ContentProvider中的数据验证无效

时间:2017-07-26 13:47:55

标签: android validation insert android-contentprovider

我尝试阻止用户传入空输入。

 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.
}

无效数据仍然可以进入数据库。

1 个答案:

答案 0 :(得分:0)

尝试以下验证:

if(!contentValues.containsKey(StudentEntry.COLUMN_NAME) || TextUtils.isEmpty(contentValues.getAsString(StudentEntry.COLUMN_NAME))) {
    throw new IllegalArgumentException("A name is required");
}