在我的应用中。我有一系列的for表。
当投球手输入分数时,id可能看起来像这样;
在我的游戏表中,我记录了每场比赛的得分(即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;
}
但是,它似乎没有生成平均值。在我的数据库中,我有3个得分,游戏1 = 222,游戏2 = 300,游戏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 :(得分:1)
您的sql语句是CREATE语句,而不是用于获取行的查询。因此,请更改它:
"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 + "'"
您可能还需要更改:total = cursor.getInt(0);
我不确定是否将产生的平均值将舍入为int。
最好使用getDouble
或getString
,然后解析为int