2014-06-26 65 views
0

嗨,我有一個問題。我如何從後向讀取我的Sql數據。我嘗試了很多的事情,我在網上找到,但沒有在這裏工作是我的代碼:從後面讀取Sql數據庫

public Cursor getAllRows() { 
    String where = null; 
    Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, 
         where, null, null, null,null,null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 

而且光標的代碼:

Cursor cursor = myDb.getAllRows(); 

// Allow activity to manage lifetime of the cursor. 
// DEPRECATED! Runs on the UI thread, OK for small/short queries. 
startManagingCursor(cursor); 

// Setup mapping from cursor to view fields: 
String[] fromFieldNames = new String[] 
     {DBAdapter.KEY_KALO, DBAdapter.KEY_HYDRATE, DBAdapter.KEY_FAT, DBAdapter.KEY_PROTEIN ,DBAdapter.KEY_CAL}; 
int[] toViewIDs = new int[] 
     {R.id.item_Kalorien,  R.id.item_KG_in_g,   R.id.item_F_in_g,  R.id.item_P_in_g,R.id.item_cal}; 

// Create adapter to may columns of the DB onto elemesnt in the UI. 
SimpleCursorAdapter myCursorAdapter = 
     new SimpleCursorAdapter(
       this,  // Context 
       R.layout.item_tagebuch, // Row layout template 
       cursor,     // cursor (set of DB records to map) 
       fromFieldNames,   // DB Column names 
       toViewIDs    // View IDs to put information in 
       ); 

// Set the adapter for the list view 
ListView myList = (ListView) findViewById(R.id.listView1); 
myList.setAdapter(myCursorAdapter); 

坦克你的幫助,爲我的壞對不起英語:)

+0

'讀我從backward'的Sql DATAS並不意味着很多,我......你的意思是'我如何以相反的順序顯示數據? –

+0

是:)對不起我不知道怎麼說 – 360Productions

+0

'cursor.moveToLast();'while while(cursor。hasPrevious()){//做你的代碼}' –

回答

1

「從落後」的說法是模糊的我假設你想在一個特定的順序

讀取數據庫,如果你想在一個領域的降序排列閱讀說「富」,然後

String ORDER = "foo DESC";/*use ASC for ascending order*/

和更改鱈如下

public Cursor getAllRows() { 
    String where = null; 
    String Order = "foo DESC"; 
    Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, 
         where, null, null, null,Order,null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 

這裏是查詢的語法

public Cursor query (String table, String[] columns, String selection,String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

參考以下鏈接瞭解詳細explanantion SQLiteDatabase Android

+0

嗯我有一個運行時異常... – 360Productions

+0

你能讓我知道什麼是例外嗎? 而不是檢查是否c!= null檢查c.getCount()是否爲0, 查詢將無論如何返回一個遊標對象,因此它不會爲空anywayz –

+0

06-26 08:32:54.433:E/SQLiteLog 979):(1)沒有這樣的列:foo – 360Productions

0

通過向後是怎樣的一個奇怪的詞,但我想你需要的數據,因此在這裏整理東西

獲取數據

這可以通過使用順序完成之前

排序通過關鍵字。 db.query()方法中的最後一個參數是order by子句(不含「order by」關鍵字)。

因此改變你的查詢作爲

Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, 
        where, null, null, null,null,COLUMN_NAME + " ASC"); 

ASC是由升序和降序的decending

排序,如果你想超過一個排序,然後做這樣的

Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, 
        where, null, null, null,null,COLUMN_NAME + " ASC, " +COLUMN_NAME2 + " DESC"); 

2.獲取數據後進行分類

如果你試圖獲取數據,那麼它應該在光標層級完成後,將其排序

Cursor cursor = myDb.getAllRows(); 
if (cursor.moveToFirst()) { 
    do { 
    // do what you need with the cursor here 
    } while (cursor.moveToNext()); 
} 

或反向

if (cursor.moveToLast()) { 
    do { 
    // do what you need with the cursor here 
    } while (cursor.moveToPrevious()); 
} 
+0

嗯,我有一個運行時異常.. :( – 360Productions

+0

嗯,如果我做的第二種方式沒有運行時異常,但它不工作:(嗯 – 360Productions

+0

嗯,我應該改變我的GetAllRow方法在第二個例子呢? @Girish奈爾 – 360Productions