我正在尝试过滤城市名称,并根据传递的选择参数返回匹配的城市。
但是查询调用仍然返回所有行。甚至尝试输入数据库中尚不存在的伪造城市名称,它仍然吐出相同的原始行。
public void addLocation(String cityName){
Cursor cursor = mContext.getResolver().query(
WeatherContract.LocationEntry.CONTENT_URI,
null,
WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING +
" = " + cityName,
null,
null);
我已经读过一遍又一遍的Android文档,ContentResolver Query,并且在其中传递了selection参数似乎没有任何作用。
public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
而不是将值传递到选择参数中,而是将其放在selectionArgs中。
public void addLocation(String cityName){
String[] args = { cityName };
Cursor cursor = mContext.getResolver().query(
WeatherContract.LocationEntry.CONTENT_URI,
null,
WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING +
" = ?",
args,
null);
假设您正在使用的提供者为androidx.core.content.FileProvider
(根据您在清单中声明的内容),则selection
的selectionArgs
,sortOrder
和FileProvider.query
参数为... 忽略在AndroidX的实现中!
请参见v1.2.0源here(第409行)
我发现的唯一解决方案是对结果进行过滤和排序查询后,用您自己的代码。是的,这有点可悲...