2012-10-16 156 views
0

我現在正在這樣做。什麼是使用SQLiteOpenHelper進行JOIN查詢的最佳方式

有沒有更好的方法?

public Cursor selectRostersForTeam(Activity activity, int teamId) { 

    final String MY_QUERY = "SELECT r." + _ROSTER_ID + ", t." + _TEAM_ID + ", t." + TEAM_NAME + ", t." + TEAM_SEASON_WINS + ", t." + TEAM_SEASON_LOSSES + ", t." + TEAM_CHAMPIONSHIP_WINS + ", j." + _TEAMS_ROSTERS_ID + " FROM " + TABLE_TEAM + " AS p" + " JOIN " + TABLE_TEAMS_ROSTERS + " j ON (r." + _ROSTER_ID + " = j." + TEAMS_ROSTER_ROSTER_ID + ") " + " JOIN " + TABLE_TEAM + " ch ON (j." + TEAMS_ROSTER_TEAM_ID + " = t." + _TEAM_ID + ") WHERE j. " + TEAMS_ROSTER_TEAM_ID + " = " + teamId + ";"; 



    SQLiteDatabase db = getReadableDatabase(); 

    Cursor cursor = db.rawQuery(MY_QUERY, null); 

    activity.startManagingCursor(cursor); 

    return cursor; 
} 

回答

1

rawQuery()另一種方法是query(),但一個是不適合加入。

如果您認爲您的加入足夠重要,那麼您可以將create a view取出來,然後在表格中將其用於query()


注:建議通過像teamId作爲參數而不是將它們格式化成的查詢字符串值:

rawQuery(..._TEAM_ID + " = ?", new String[] { Integer.toString(teamId) }); 
相關問題