Android: 如何使用 ContextResolver 在 sqlite 中进行内连接查询?
Android: how to do an inner-join query in sqlite using the ContextResolver?
我想创建一个 sqlite 游标,您可以使用 contextResolver 处理内部连接查询。
这是我当前的光标:
Cursor cursor = getContentResolver().query(Exercise.CONTENT_URI, new String[]{Exercise.Columns._ID, Exercise.Columns.EXERCISE_NAME, Exercise.Columns.DONE_LAST},
"",null, Exercise.Columns.DONE_LAST);
我的新 Cusor 应该处理这样的事情:
private final String MY_QUERY =
"SELECT a."+Exercise.Columns.EXERCISE_NAME+", a."+Exercise.Columns.DONE_LAST+", b."+Progress.Columns.WEIGHT+", " +
"b."+Progress.Columns.SETS+", b."+Progress.Columns.REPITITIONS+" " +
"FROM "+Exercise.TABLE_NAME+" a " +
"INNER JOIN "+Progress.TABLE_NAME+" b " +
"ON a.+"+Exercise.Columns._ID+" = b."+Progress.Columns._ID +
"WHERE b.+"+Progress.Columns.WHENDONE+" = ( Select MAX (b."+Progress.Columns.WHENDONE+") from b";
如果在 SOV 中找到一些解决方案 - 它们都使用 rawQuery,但我的 activity 中没有 SQLiteOpenHelper 对象。那么是否有另一种解决方案,通过 getContentResolver() 执行此查询?
您将在两个数据库表上创建持久视图。然后从内容提供者访问视图。如果视图不存在,您也可以创建它,但效率不如已经构建的视图。
private static final String VERSION_66_CREATE_VIEW = "CREATE VIEW IF NOT EXISTS "
+ VIEW_MARKERS
+ " AS SELECT "
+ TABLE_MARKERS
+ "."
+ KEY_ID
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_LOCATION_ID
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_ID
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_SNIPPET
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_PROCESSED_ID
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_URL
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_URL_THUMBLARGE
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_URL_THUMBMEDIUM
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_URL_THUMBSMALL
+ ", "
+ TABLE_MARKERS + "." + KEY_MARKER_TITLE + ", "
+ TABLE_MARKERS + "." + KEY_MARKER_ALBUM_ID + ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_LATITUDE
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_LONGITUDE
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_ACCURACY
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_ALTITUDE
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_BEARING
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_PROVIDER
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_SPEED
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_TIME
+ " FROM "
+ TABLE_MARKERS
+ " JOIN "
+ TABLE_LOCATIONS
+ " ON "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_LOCATION_ID
+ " = "
+ TABLE_LOCATIONS + "." + KEY_ID;
db.execSQL(VERSION_66_CREATE_VIEW);
访问 ContentProvider 中的视图
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// Uisng SQLiteQueryBuilder instead of query() method
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
// check if the caller has requested a column which does not exists
// Set the table
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case MARKERS:
// create the view here if you can't change it in the database.
checkColumns(availableMarkers, projection);
queryBuilder.setTables(RidesDatabaseHandler.VIEW_MARKERS);
break;
...
SQLiteDatabase db = database.getWritableDatabase();
Cursor cursor = queryBuilder.query(db, projection, selection,
selectionArgs, null, null, sortOrder);
// make sure that potential listeners are getting notified
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
我想创建一个 sqlite 游标,您可以使用 contextResolver 处理内部连接查询。
这是我当前的光标:
Cursor cursor = getContentResolver().query(Exercise.CONTENT_URI, new String[]{Exercise.Columns._ID, Exercise.Columns.EXERCISE_NAME, Exercise.Columns.DONE_LAST},
"",null, Exercise.Columns.DONE_LAST);
我的新 Cusor 应该处理这样的事情:
private final String MY_QUERY =
"SELECT a."+Exercise.Columns.EXERCISE_NAME+", a."+Exercise.Columns.DONE_LAST+", b."+Progress.Columns.WEIGHT+", " +
"b."+Progress.Columns.SETS+", b."+Progress.Columns.REPITITIONS+" " +
"FROM "+Exercise.TABLE_NAME+" a " +
"INNER JOIN "+Progress.TABLE_NAME+" b " +
"ON a.+"+Exercise.Columns._ID+" = b."+Progress.Columns._ID +
"WHERE b.+"+Progress.Columns.WHENDONE+" = ( Select MAX (b."+Progress.Columns.WHENDONE+") from b";
如果在 SOV 中找到一些解决方案 - 它们都使用 rawQuery,但我的 activity 中没有 SQLiteOpenHelper 对象。那么是否有另一种解决方案,通过 getContentResolver() 执行此查询?
您将在两个数据库表上创建持久视图。然后从内容提供者访问视图。如果视图不存在,您也可以创建它,但效率不如已经构建的视图。
private static final String VERSION_66_CREATE_VIEW = "CREATE VIEW IF NOT EXISTS "
+ VIEW_MARKERS
+ " AS SELECT "
+ TABLE_MARKERS
+ "."
+ KEY_ID
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_LOCATION_ID
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_ID
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_SNIPPET
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_PROCESSED_ID
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_URL
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_URL_THUMBLARGE
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_URL_THUMBMEDIUM
+ ", "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_IMAGE_URL_THUMBSMALL
+ ", "
+ TABLE_MARKERS + "." + KEY_MARKER_TITLE + ", "
+ TABLE_MARKERS + "." + KEY_MARKER_ALBUM_ID + ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_LATITUDE
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_LONGITUDE
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_ACCURACY
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_ALTITUDE
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_BEARING
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_PROVIDER
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_SPEED
+ ", "
+ TABLE_LOCATIONS
+ "."
+ KEY_LOCATION_TIME
+ " FROM "
+ TABLE_MARKERS
+ " JOIN "
+ TABLE_LOCATIONS
+ " ON "
+ TABLE_MARKERS
+ "."
+ KEY_MARKER_LOCATION_ID
+ " = "
+ TABLE_LOCATIONS + "." + KEY_ID;
db.execSQL(VERSION_66_CREATE_VIEW);
访问 ContentProvider 中的视图
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// Uisng SQLiteQueryBuilder instead of query() method
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
// check if the caller has requested a column which does not exists
// Set the table
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case MARKERS:
// create the view here if you can't change it in the database.
checkColumns(availableMarkers, projection);
queryBuilder.setTables(RidesDatabaseHandler.VIEW_MARKERS);
break;
...
SQLiteDatabase db = database.getWritableDatabase();
Cursor cursor = queryBuilder.query(db, projection, selection,
selectionArgs, null, null, sortOrder);
// make sure that potential listeners are getting notified
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;