gpt4 book ai didi

java - 使用 SQLiteOpenHelper 时进行 JOIN 查询的最佳方法是什么

转载 作者:行者123 更新时间:2023-12-01 15:06:13 28 4
gpt4 key购买 nike

我目前正在这样做。

有更好的方法吗?

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;
}

最佳答案

rawQuery() 的替代方案是 query(),但它不是为连接而设计的。

如果您认为您的加入足够重要,您可以 create a view出来,然后在表中使用它进行 query()

<小时/>

注意:建议将 teamId 之类的值作为参数传递,而不是将它们格式化为查询字符串:

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

关于java - 使用 SQLiteOpenHelper 时进行 JOIN 查询的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12914088/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com