我试图通过在ContentResolver的查询的选择字符串中定义 SQL WHERE子句来限制数据库结果。
Cursor cursor = getContentResolver().query(uri, null, getSelectionString(), null, null);
...
public String getSelectionString() {
// TODO Replace latitude and longitude with database reference.
StringBuilder string = new StringBuilder();
string.append("latitude >= ").append(northEast.getLatitudeE6() / 1e6);
string.append(" AND ");
string.append("latitude <= ").append(southWest.getLatitudeE6() / 1e6);
string.append(" AND ");
string.append("longitude >= ").append(southWest.getLongitudeE6() / 1e6);
string.append(" AND ");
string.append("longitude <= ").append(northEast.getLongitudeE6() / 1e6);
return string.toString();
}
数据库列定义如下...
public class CustomDatabase {
public static final class Contract {
public static final String COLUMN_NAME = "name";
public static final String COLUMN_LATITUDE = "latitude";
public static final String COLUMN_LONGITUDE = "longitude";
}
}
...
我不是特别确定我可以检查游标中发送的查询。如果是这样,它不包含我发送的 WHERE 子句:
SQLiteQuery: SELECT * FROM custom_db ORDER BY number ASC
以下是选择字符串的示例:
latitude >= 48.203927 AND latitude <= 48.213851 AND longitude >= 16.36735 AND longitude <= 16.377648
问题:
- WHERE 子句在语法上是否正确?
- 我需要撇号吗?
- 我是否被迫在查询中一次使用两个参数(selection、selectionArgs)?
- 我在哪里可以调试整个查询?
编辑:
这是ContentProvider的query()方法...
public class CustomProvider extends ContentProvider {
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
switch (URI_MATCHER.match(uri)) {
case URI_CODE_LOCATIONS:
return mCustomDatabase.getLocations();
}
return null;
}
...显然,正如biegleux猜测的那样,我忘了传递参数。嗬!
这是数据库方法的当前实现...
public class CustomSQLiteOpenHelper extends SQLiteOpenHelper {
public Cursor getLocation() {
return mDatabaseHelper.getReadableDatabase().query(
CustomSQLiteOpenHelper.TABLE_NAME,
null, null, null, null, null, null);
}
您是否建议我将方法签名更改为以下以传递所有参数?我不是以这种方式暴露了大部分数据库接口吗?
public Cursor getLocations(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
return mDatabaseHelper.getReadableDatabase().query(
CustomSQLiteOpenHelper.TABLE_NAME,
columns, selection, selectionArgs, groupBy, having, orderBy);
}