【发布时间】:2018-12-31 08:43:18
【问题描述】:
在我的应用程序中。我有一系列的表格。
- 联赛
- 投球手
- 系列
- 游戏
当投球手输入他/她的分数时,id 可能看起来像这样;
- 联赛 ID = 2
- 投球手 id = 1
- 系列 id = 1
- 游戏 ID = 1,2,3
在我的游戏表中,我记录了每场比赛的得分(即 1、2、3)。我希望能够生成一个系列的平均分数(即 1)。
我知道这可以用
CREATE VIEW AVERAGE_SCORE AS SELECT AVG(COLUMN_SCORE) FROMFROM " + Game.TABLE_NAME + " WHERE " + Game.COLUMN_LEAGUE_ID + " = '" + leagueId + "'" + " AND " + Game.COLUMN_BOWLER_ID + " = '" + bowlerId + "'" + " AND " + Game.COLUMN_SERIES_ID + " = '" + seriesId + "'" + " ORDER BY " +
Game.COLUMN_TIMESTAMP + " DESC";
我已设法使用以下代码在 logcat 中显示一个值:
public int getSeriesAverage(String leagueId, String bowlerId, String seriesId)
{
int total = 0;
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database.rawQuery("CREATE VIEW IF NOT EXISTS AVERAGE_SCORE AS SELECT AVG(COLUMN_SCORE) FROM " + Game.TABLE_NAME + " WHERE " + Game.COLUMN_LEAGUE_ID + " = '" + leagueId + "'" + " AND " + Game.COLUMN_BOWLER_ID + " = '" + bowlerId + "'" + " AND " + Game.COLUMN_SERIES_ID + " = '" + seriesId + "'" + " ORDER BY " +
Game.COLUMN_TIMESTAMP + " DESC", null);
if (cursor.moveToFirst()) {
do {
total = cursor.getInt(0);
}while (cursor.moveToNext());
}
cursor.close();
//Close Database Connection
database.close();
Log.d("GET AVERAGE FROM SQL","Average = >>>>" + total + "<<<<");
return total;
}
但是,它似乎没有生成平均值。在我的数据库中,我有三个分数 Game 1 = 222、Game 2 = 300、Game 3 = 200。平均值应该是 240。但 Log.d 显示为 0。
07-24 19:58:53.385 7287-7287/ca.rvogl.tpbcui D/GET AVERAGE FROM SQL: Average = >>>>0<<<<
我做错了什么,任何帮助将不胜感激
更改代码:
public int getSeriesAverage(String leagueId, String bowlerId, String seriesId)
{
int total = 0;
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database.rawQuery("SELECT AVG(COLUMN_SCORE) FROM " + Game.TABLE_NAME + " WHERE " + Game.COLUMN_LEAGUE_ID + " = '" + leagueId + "'" + " AND " + Game.COLUMN_BOWLER_ID + " = '" + bowlerId + "'" + " AND " + Game.COLUMN_SERIES_ID + " = '" + seriesId + "'", null);
if (cursor.moveToFirst()) {
do {
total = cursor.getInt(0);
}while (cursor.moveToNext());
}
cursor.close();
//Close Database Connection
database.close();
Log.d("GET AVERAGE FROM SQL","Average = >>>>" + total + "<<<<");
return total;
}
系列类:
public class Series {
public static final String TABLE_NAME = "Series";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_LEAGUE_ID = "league_id";
public static final String COLUMN_BOWLER_ID = "bowler_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_SERIES_AVERAGE = "average";
public static final String COLUMN_TIMESTAMP = "timestamp";
private int id;
private String league_id;
private String bowler_id;
private String name;
private String average;
private String timestamp;
// Create table SQL query
public static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_LEAGUE_ID + " TEXT,"
+ COLUMN_BOWLER_ID + " TEXT,"
+ COLUMN_NAME + " TEXT,"
+ COLUMN_SERIES_AVERAGE + " TEXT,"
+ COLUMN_TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP"
+ ")";
public Series() {
}
public Series(int id, String league_id, String bowler_id, String name, String average, String timestamp) {
this.id = id;
this.league_id = league_id;
this.bowler_id = bowler_id;
this.name = name;
this.average = average;
this.timestamp = timestamp;
}
public int getId() {
return id;
}
public String getLeagueId() {return league_id;}
public String getBowlerId() {return bowler_id;}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAverage() {
return average;
}
public void setAverage(String average) {
this.average = average;
}
public String getTimestamp() {
return timestamp;
}
public void setId(int id) {
this.id = id;
}
public void setLeagueId(String league_id) {
this.league_id = league_id;
}
public void setBowlerId(String bowler_id) {
this.bowler_id = bowler_id;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
更新方法:
public int getSeriesAverage(String leagueId, String bowlerId, String seriesId)
{
int total = 0;
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database.rawQuery("SELECT AVG(" + Game.COLUMN_SCORE + ") FROM " + Game.TABLE_NAME + " WHERE " + Game.COLUMN_LEAGUE_ID + " = '" + leagueId + "'" + " AND " + Game.COLUMN_BOWLER_ID + " = '" + bowlerId + "'" + " AND " + Game.COLUMN_SERIES_ID + " = '" + seriesId + "'", null);
if (cursor.moveToFirst()) {
do {
total = cursor.getInt(0);
}while (cursor.moveToNext());
}
cursor.close();
//Close Database Connection
database.close();
Log.d("GET AVERAGE FROM SQL","Average = >>>>" + total + "<<<<");
ContentValues values = new ContentValues();
values.put(Series.COLUMN_SERIES_AVERAGE,Series.getAverage());
Log.d("GET AVERAGE FROM SQL","Average = >>>>" + Series.COLUMN_SERIES_AVERAGE + "<<<<");
return total;
}
【问题讨论】:
-
您是否调试过这个 0 是否来自
int total = 0;,因为游标没有返回任何行,或者它确实返回了 1 行,平均值为 0?另一件事:您不需要所有部分的顺序,您不想按特定顺序获取行而是单个值 -
是的,我已经调试过了,int total 确实返回了 0。我在上面添加了一个屏幕截图。
标签: javascript android sqlite android-studio create-view