我如何订购我的SQLITE数据库降序,为一个Android应用程序?
以降序显示我的数据的最有效的方法是什么?
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 }
查询有两种语法,你使用的语法,最后一列代表顺序,你只需要指定你想要做什么列orderby +“ASC”(或)orderby +“DESC”
Cursor c = scoreDb.query(DATABASE_TABLE, rank, null, null, null, null, yourColumn+" DESC");
请参阅此文档以了解更多关于查询方法。
Cursor c = scoreDb.query(Table_Name, score, null, null, null, null, Column+" DESC");
尝试这个
return database.rawQuery("SELECT * FROM " + DbHandler.TABLE_ORDER_DETAIL + " ORDER BY "+DbHandler.KEY_ORDER_CREATED_AT + " DESC" , new String[] {});
根据文档 :
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);
和你的ORDER BY参数意味着:
如何对行进行sorting,格式为SQL ORDER BY子句(不包括ORDER BY本身)。 传递null将使用默认的sorting顺序,可能是无序的。
所以,你的查询将是:
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, KEY_ITEM + " DESC", null);
Cursor c = myDB.rawQuery("SELECT distinct p_name,p_price FROM products order by Id desc",new String[]{});
这适用于我!
我们还有一个选项可以做
public Cursor getlistbyrank(String rank) { try { //This can be used return db.`query("tablename", null, null, null, null, null, rank +"DESC",null ); OR return db.rawQuery("SELECT * FROM table order by rank", null); } catch (SQLException sqle) { Log.e("Exception on query:-", "" + sqle.getMessage()); return null; } }
您可以使用这两种方法进行sorting
你可以这样做
Cursor cursor = database.query( TABLE_NAME, YOUR_COLUMNS, null, null, null, null, COLUMN_INTEREST+" DESC");
public List getExpensesList(){ SQLiteDatabase db = this.getWritableDatabase(); List<String> expenses_list = new ArrayList<String>(); String selectQuery = "SELECT * FROM " + TABLE_NAME ; Cursor cursor = db.rawQuery(selectQuery, null); try{ if (cursor.moveToLast()) { do{ String info = cursor.getString(cursor.getColumnIndex(KEY_DESCRIPTION)); expenses_list.add(info); }while (cursor.moveToPrevious()); } }finally{ cursor.close(); } return expenses_list; }
这是我从数据库中读取列表视图的方式,以降序排列。 将光标移到最后,并在获取每个logging后移到上一个logging。 希望这有助于〜
关于高效的方法。 你可以使用CursorLoader。 例如,我包括我的行动。 而且你必须为你的数据库实现ContentProvider。 https://developer.android.com/reference/android/content/ContentProvider.html
如果你实现这个,你会打电话给你的数据库非常有效。
public class LoadEntitiesActionImp implements LoaderManager.LoaderCallbacks<Cursor> { public interface OnLoadEntities { void onSuccessLoadEntities(List<Entities> entitiesList); } private OnLoadEntities onLoadEntities; private final Context context; private final LoaderManager loaderManager; public LoadEntitiesActionImp(Context context, LoaderManager loaderManager) { this.context = context; this.loaderManager = loaderManager; } public void setCallback(OnLoadEntities onLoadEntities) { this.onLoadEntities = onLoadEntities; } public void loadEntities() { loaderManager.initLoader(LOADER_ID, null, this); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader(context, YOUR_URI, null, YOUR_SELECTION, YOUR_ARGUMENTS_FOR_SELECTION, YOUR_SORT_ORDER); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { } @Override public void onLoaderReset(Loader<Cursor> loader) { }
这是一件可怕的事情! 这花了我几个小时! 这是我的表格行:
private String USER_ID = "user_id"; private String REMEMBER_UN = "remember_un"; private String REMEMBER_PWD = "remember_pwd"; private String HEAD_URL = "head_url"; private String USER_NAME = "user_name"; private String USER_PPU = "user_ppu"; private String CURRENT_TIME = "current_time"; Cursor c = db.rawQuery("SELECT * FROM " + TABLE +" ORDER BY " + CURRENT_TIME + " DESC",null);