在行数据库中从一端移动到另一端

时间:2015-02-12 05:31:07

标签: android database

我在我的应用程序中创建了一个数据库。 并获取行的值,使用以下函数。

数据库:

qustion|answer_low|
  q1         a1
  q2         a2
  q3         a3

方法:

  public void getValueRow()
 {
    ArrayList<String> arr_q=new ArrayList<String>();
    ArrayList<String> arr_al=new ArrayList<String>();

    SQLiteDatabase dataBase=openOrCreateDatabase(path, MODE_PRIVATE, null); 
    Cursor cursor;
    cursor=dataBase.rawQuery("SELECT qustion,,answer_low,", null);    
        while (cursor.moveToNext())
        {
         arr_q.add(cursor.getString(cursor.getColumnIndex("qustion")));                  
        arr_al.add(cursor.getString(cursor.getColumnIndex("answer_low")));


          }
        }
        dataBase.close();
    }

此函数获取数据库的值。 并存储在ArrayList中(如下所示)

 index   0   |1  |  2
 arr_q   q1   q2    q3

但我想要价值观 从头到尾保存

   index   0   |1  |  2
   arr_q   q3   q2    q1

我的代码应该更改什么?

4 个答案:

答案 0 :(得分:2)

已使用cursor.moveToLast()

这会将光标移动到最后一行。

像这样

     cursor.moveToLast()

     while (cursor.moveToPrevious())
    {
     arr_q.add(cursor.getString(cursor.getColumnIndex("qustion")));                  
     arr_al.add(cursor.getString(cursor.getColumnIndex("answer_low")));

      }

答案 1 :(得分:0)

for (cursor.moveToLast(); !cursor.isBeforeFirst(); cursor.moveToPrevious()) {


   arr_q.add(cursor.getString(cursor.getColumnIndex("qustion")));                  
     arr_al.add(cursor.getString(cursor.getColumnIndex("answer_low")));




    // get stuff from the cursor 
}

答案 2 :(得分:0)

我会更改查询以使用ORDER BY子句以正确的顺序显示结果。

cursor=dataBase.rawQuery("SELECT qustion,,answer_low ORDER BY qustion",null);

答案 3 :(得分:-1)

您可以使用cursor.moveToLast()将控件移动到最后一行,然后使用cursor.moveToPrevious()

进行迭代
cursor.moveToLast();

while (cursor.moveToPrevious())
{
    arr_q.add(cursor.getString(cursor.getColumnIndex("qustion")));                  
    arr_al.add(cursor.getString(cursor.getColumnIndex("answer_low")));


}

参考Cursor课程