【发布时间】:2018-02-06 20:09:39
【问题描述】:
在为 Android 编程时,我第一次偶然发现了 INNER JOIN 与 SqlLite 的概念。 我设法找回了一些东西,但现在的问题是我想在列表中显示数据,而我无法按预期显示信息。
这是检索数据的查询
cursor = database.rawQuery("SELECT matchesbet._id, hometeam, awayteam, gamble " +
"FROM matchesbet INNER JOIN gambles ON matchesbet.idmatch = gambles.idmatch " +
"WHERE matchesbet.idbet = ? GROUP BY matchesbet.idmatch ORDER BY matchesbet._id DESC", selectionArgs);
数据用于在 bindView 方法中扩展 CursorAdapter 的类中
TextView tvGamble = (TextView) view.findViewById(R.id.gamble_text_view);
// Extract properties from cursor
int gamble = cursor.getInt(cursor.getColumnIndex("gamble"));
tvGamble.setText(String.valueOf(gamble));
现在在数据库中,我为每场比赛保存了多个“赌博”,但我唯一可以显示的是 1 场赌博。
数据库表结构和样本数据
MATCHESBET
+-----+-------+---------+----------+-----------+--+
| id | idbet | idmatch | hometeam | awayteam | |
+-----+-------+---------+----------+-----------+--+
| 349 | 3000 | 27 | TeamA | TeamZ | |
| 350 | 2456 | 39 | TeamC | TeamG | |
| 351 | 2818 | 20 | TeamW | TeamB | |
+-----+-------+---------+----------+-----------+--+
GAMBLES
+-----+---------+--------+-------+
| _id | idmatch | gamble | idbet |
+-----+---------+--------+-------+
| 349 | 27 | WIN | 3000 |
| 350 | 27 | LOST | 3000 |
| 351 | 27 | DRAW | 3000 |
| 489 | 1345 | WIN | 1981 |
+-----+---------+--------+-------+
考虑到我过滤了 ID = 3000 的 Bet,基于上述数据,我想要一个包含 TeamA 与 TeamZ(ID = 27)的 listItem,并且在同一行中包含字符串 WIN、LOST、DRAW。然后再次遍历 IDBET = 3000
的所有匹配项我不知道如何摆脱这种情况 希望有人可以帮助 干杯
【问题讨论】:
-
如果添加行(检索光标后)
Log.d("CRSCOUNT","Number of rows in Cursor is " + cursor.getCount());,运行并且计数不大于 1,那么很可能是 WHERE 条件造成的。如果是这样,那么也许尝试使用cursor = database.rawQuery("SELECT matchesbet._id, hometeam, awayteam, gamble " + "FROM matchesbet INNER JOIN gambles ON matchesbet.idmatch = gambles.idmatch " + "GROUP BY matchesbet.idmatch ORDER BY matchesbet._id DESC", null); -
我唯一注意到的是,通过删除 GROUP BY,我将检索来自 gambles 表的 3 个元素。但是一旦我显示我在列表中有 3 个元素。我想要实现的是一个 List 元素,并且在 ListView 中包含的 textView 中包含 gambles 表的 3 个元素。
-
我忘记了 WHERE 条件对于从 matchsbet 表中提取正确记录是必需的。
-
能否用一些示例数据添加两张表的布局。
-
我希望现在更清楚一点。
标签: join android-sqlite android-cursoradapter