Android从SQLite数据库检索值
我不熟悉Android和SQLite数据库。我正在完成一个教程,但我在将其应用于我的应用程序时遇到了问题 我想建立一个高分数据库,每个级别都有高分。因此,我的表将被称为HighScore,字段id、Level和HighScore。Level和HighScore是整数 以下是我的课程:Android从SQLite数据库检索值,android,sqlite,Android,Sqlite,我不熟悉Android和SQLite数据库。我正在完成一个教程,但我在将其应用于我的应用程序时遇到了问题 我想建立一个高分数据库,每个级别都有高分。因此,我的表将被称为HighScore,字段id、Level和HighScore。Level和HighScore是整数 以下是我的课程: 公共课高分{ 私有int-id; 私有整数级; 私人整数高分; 公共高分(){} 公共高分(整数级别,整数高分){ 超级(); 这个。级别=级别; this.highscore=高分; } //接球手和接球手 公
公共课高分{
私有int-id;
私有整数级;
私人整数高分;
公共高分(){}
公共高分(整数级别,整数高分){
超级();
这个。级别=级别;
this.highscore=高分;
}
//接球手和接球手
公共整数getHighScore(){
返回高分;
}
公共整数getLevel(){
回报水平;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共void setLevel(整数级){
这个。级别=级别;
}
公共无效设置高分(整数高分){
this.highscore=高分;
}
}
只需将getHighScore更改如下
public int getHighScore(Integer Level){
// 1. get reference to readable DB
SQLiteDatabase db = this.getReadableDatabase();
// 2. build query
Cursor cursor =
db.query(TABLE_HIGHSCORES, // a. table
COLUMNS, // b. column names
" Level = ?", // c. selections
new String[] { String.valueOf(Level) }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
// 4. return highScore
return cursor.getInt(2);
}
不要创建
HighScore
对象,只需返回HighScore
的Integer
值即可。真管用!不幸的是,我不得不等7分钟才能把这篇文章标记为答案。再次感谢。