我正在尝试编写一个查询,用于返回用户从SQLite数据库中播放的游戏会话数量。 (即SessionID列的计数)
我希望将此实现为嵌入了Query的方法。
我的目标是基于这个有点类似的方法,它返回数据库中的所有信息,即:
// Getting All scores
public List<Score> getAllScores() {
List<Score> scoreList = new ArrayList<Score>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_SCORE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Score score = new Score();
score.setSessionID(cursor.getString(0));
score.setGameTitle(cursor.getString(1));
score.setName(cursor.getString(2));
score.setMeditation(Integer.parseInt(cursor.getString(3)));
score.setMax(Integer.parseInt(cursor.getString(4)));
score.setAvgAttention(Integer.parseInt(cursor.getString(5)));
score.setMaxAttention(Integer.parseInt(cursor.getString(6)));
score.setScore(Integer.parseInt(cursor.getString(7)));
score.setDate(cursor.getString(8));
// Adding contact to list
scoreList.add(score);
} while (cursor.moveToNext());
}
// return contact list
return scoreList;
}
这是我到目前为止的尝试:
public int getTotalGamesPlayed() {
List<Score> scoreList = new ArrayList<Score>();
// Select All Query
String selectQuery = "SELECT COUNT FROM " + COL_SESSION;
//is this needed?
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// int to return = return value of the query?
}
我如何更正以便实际运作?
供参考,数据库结构:
// Database Version
private static final int DATABASE_VERSION = 10;
// Database Name
private final static String DATABASE_NAME = "MeditationDatabase";
// Contacts table name
private static final String TABLE_SCORE = "scores";
// Contacts Table Columns names
private static final String COL_SESSION = "sessionid";
private static final String COL_GAMETITLE = "game";
private static final String COL_NAME = "name";
private static final String COL_MED = "avgmeditation";
private static final String COL_MAX = "maxmeditation";
private static final String COL_AVGATT = "avgattention";
private static final String COL_MAXATT = "maxattention";
private static final String COL_SCORE = "score";
private static final String COL_DATE = "date";
/**
* Constructor
*
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/**
* Method that creates the database
*/
@Override
public void onCreate(SQLiteDatabase db) {
//VERY IMPORTANT: ALWAYS CHECK THAT THERE ARE SPACES AND COMMAS IN CORRECT PLACE IN CODE BELOW:
String CREATE_TABLE_SCORE = "CREATE TABLE " + TABLE_SCORE + "(" + COL_SESSION
+ " STRING PRIMARY KEY, " + COL_GAMETITLE + " STRING, " + COL_NAME + " STRING, " + COL_MED + " INTEGER, "
+ COL_MAX + " INTEGER, " + COL_AVGATT + " INTEGER, " + COL_MAXATT + " INTEGER, " + COL_SCORE + " INTEGER, " + COL_DATE + " STRING " + ")";
db.execSQL(CREATE_TABLE_SCORE);
}
答案 0 :(得分:1)
COUNT
是一个函数,它需要一个指定要计数的参数。
要计算整行,请使用*
,如下所示:
SELECT COUNT(*) FROM scores
然而,有helper function简化了这一点:
public int getTotalGamesPlayed() {
SQLiteDatabase db = this.getWritableDatabase();
try {
return (int)DatabaseUtils.queryNumEntries(db, TABLE_SCORE);
} finally {
db.close();
}
}
答案 1 :(得分:0)
您甚至可以实际使用光标中的内置函数。如果您想从表Scores中获取行数,请使用以下代码:
public int getTotalGamesPlayed() {
List<Score> scoreList = new ArrayList<Score>();
// Select All Query
String selectQuery = "SELECT * FROM " + COL_SESSION;
//is this needed?
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
return cursor.getCount();
}
从未测试过代码,但希望它有效!