[英]How to do inner join in SQLite with multiple database helpers classes in android
[英]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,但我的活動中沒有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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.