什么是表示以降序我的数据的最有效的方法?
公共字符串getRank(){
的String []排名=新的String [] {KEY_ROWID};
光标C = scoreDb.query(DATABASE_TABLE,等级,NULL,NULL,NULL,NULL,NULL); //从数据库读取信息。
字符串rankResult =;
INT iRow = c.getColumnIndex(KEY_ROWID); //光标在寻找列设置等于这些整数。
为(c.moveToFirst();!c.isAfterLast(); c.moveToNext()){
//移动到第一行 - 在光标开始并移动到下一行,只要它不是最后一行之后。
rankResult = rankResult + c.getString(iRow)+\ N的;
//返回行的价值,这是目前。
}
返回rankResult; //返回结果
}
公共字符串的getName(){
的String [] NAME =新的String [] {} KEY_NAME;
光标C = scoreDb.query(DATABASE_TABLE,姓名,NULL,NULL,NULL,NULL,NULL); //从数据库读取信息。
字符串nameResult =;
INT iRow1 = c.getColumnIndex(KEY_NAME); //光标在寻找列设置等于这些整数。
为(c.moveToFirst();!c.isAfterLast(); c.moveToNext()){
//移动到第一行 - 在光标开始并移动到下一行,只要它不是最后一行之后。
nameResult = nameResult + c.getString(iRow1)+\ N的;
//返回行的价值,这是目前。
}
返回nameResult; //返回结果
}
公共字符串getScore(){
的String []得分=新的String [] {KEY_SCORE};
光标C = scoreDb.query(DATABASE_TABLE,得分,NULL,NULL,NULL,NULL,NULL); //从数据库读取信息。
字符串scoreResult =;
INT iRow2 = c.getColumnIndex(KEY_SCORE); //光标在寻找列设置等于这些整数。
为(c.moveToFirst();!c.isAfterLast(); c.moveToNext()){
//移动到第一行 - 在光标开始并移动到下一行,只要它不是最后一行之后。
scoreResult = scoreResult + c.getString(iRow2)+\ N的;
//返回行的价值,这是目前。
}
返回scoreResult; //返回结果
}
解决方案
查询有两种语法,你正在使用的语法,最后一列重presents订购由,你只需要指定你想要做的事列排序依据+ASC(或)排序依据+降序
光标C = scoreDb.query(DATABASE_TABLE,等级,NULL,NULL,NULL,NULL,yourColumn +降序);
请参照this文档更加了解查询方法。
What is the most efficient method of showing my data in descending order?
public String getRank() {
String[] rank = new String[]{ KEY_ROWID };
Cursor c = scoreDb.query(DATABASE_TABLE, rank, null, null, null, null, null); //reading information from db.
String rankResult = "";
int iRow = c.getColumnIndex(KEY_ROWID); //Cursor looking for column setting equal to these ints.
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//Move to first row - where cursor starts and moves to next row as long it is not after last row.
rankResult = rankResult + c.getString(iRow) + "\n";
//Returning value of row that it is currently on.
}
return rankResult; //returning result
}
public String getName() {
String[] name = new String[]{ KEY_NAME };
Cursor c = scoreDb.query(DATABASE_TABLE, name, null, null, null, null, null); //reading information from db.
String nameResult = "";
int iRow1 = c.getColumnIndex(KEY_NAME); //Cursor looking for column setting equal to these ints.
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//Move to first row - where cursor starts and moves to next row as long it is not after last row.
nameResult = nameResult + c.getString(iRow1) + "\n";
//Returning value of row that it is currently on.
}
return nameResult; //returning result
}
public String getScore() {
String[] score = new String[]{ KEY_SCORE };
Cursor c = scoreDb.query(DATABASE_TABLE, score, null, null, null,null, null); //reading information from db.
String scoreResult = "";
int iRow2 = c.getColumnIndex(KEY_SCORE); //Cursor looking for column setting equal to these ints.
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//Move to first row - where cursor starts and moves to next row as long it is not after last row.
scoreResult = scoreResult + c.getString(iRow2) + "\n";
//Returning value of row that it is currently on.
}
return scoreResult; //returning result
}
解决方案
Query has two syntax, the syntax you are using, last column represents order by, you just need to specify on what column you want to do orderby +"ASC" (or) orderby +"DESC"
Cursor c = scoreDb.query(DATABASE_TABLE, rank, null, null, null, null, yourColumn+" DESC");
Refer this documentation to understand more about query method.