Android SQLite:如何使用带有Order By和Descending功能的rawquery?

时间:2014-10-02 08:14:34

标签: php android database sqlite

我对android编程并不熟悉,但我仍然可以理解代码,我唯一的问题是我无法获得Android eclipse上我的游戏应用所需的输出,我只想从我的数据库中检索数据显示来自3个不同文本字段的高分,例如:

TextField1 <-- this will be the highest score of a player will be displayed
TextField2 <-- this will be the second highest next to textfield1
TextField3 <-- this will be the third highest score

我真的没有运气编码我认为这很容易就像在PHP中做事情但对我来说很糟糕我是一个Android的新手,如果有人可以帮助我请做:)

顺便说一句:这是我一直在工作的代码,但我没有运气来展示我想要的东西

try{ 
db = openOrCreateDatabase("DodgerDB",MODE_PRIVATE,null); 

Cursor c = db.rawQuery("select * from ScoresTable order by scoreVALUE desc",null);

while(c.moveToNext()){ 
scoreid = c.getInt(c.getColumnIndex("scoreID"));

score = c.getInt(c.getColumnIndex("scoreVALUE")); 

date = c.getString(c.getColumnIndex("scoreDATE")); 


ScoreView1.setText("score id:"+scoreid+" score:"+score+" date:"+date); 

ScoreView2.setText("score id:"+scoreid+" score:"+score+" date:"+date); 

ScoreView3.setText("score id:"+scoreid+" score:"+score+" date:"+date); 

}//end of while loop }//end of try statement 

catch(Exception er) { 
Toast.makeText(ScoresActivity.this, ""+er, Toast.LENGTH_SHORT).show(); 
}

我仍然只有来自这3个字段的数据库的相同的缩进(数据)输出请帮助:(

1 个答案:

答案 0 :(得分:0)

在这种情况下,您将只看到光标的最后一行。每次迭代都会从Cursor中检索一行,每次都会覆盖TextView中的先前值。

试试这个以了解它的工作原理:

while(c.moveToNext()){ 
scoreid = c.getInt(c.getColumnIndex("scoreID"));

score = c.getInt(c.getColumnIndex("scoreVALUE")); 

date = c.getString(c.getColumnIndex("scoreDATE")); 

Log.d("SomeTag", "score id:"+scoreid+" score:"+score+" date:"+date); 

}

UPD:在不同的TextView中显示3行:

c.moveToNext();
scoreid = c.getInt(c.getColumnIndex("scoreID"));
score = c.getInt(c.getColumnIndex("scoreVALUE")); 
date = c.getString(c.getColumnIndex("scoreDATE")); 
ScoreView1.setText("score id:"+scoreid+" score:"+score+" date:"+date); 

c.moveToNext();
scoreid = c.getInt(c.getColumnIndex("scoreID"));
score = c.getInt(c.getColumnIndex("scoreVALUE")); 
date = c.getString(c.getColumnIndex("scoreDATE")); 
ScoreView2.setText("score id:"+scoreid+" score:"+score+" date:"+date); 

c.moveToNext();
scoreid = c.getInt(c.getColumnIndex("scoreID"));
score = c.getInt(c.getColumnIndex("scoreVALUE")); 
date = c.getString(c.getColumnIndex("scoreDATE")); 
ScoreView3.setText("score id:"+scoreid+" score:"+score+" date:"+date);