【问题标题】:How to retrieve SQLite database table values from bottom to top (last row to first row)如何从下到上检索 SQLite 数据库表值(最后一行到第一行)
【发布时间】:2012-05-05 18:44:30
【问题描述】:

我正在尝试将 sqlite 表的元素检索到黑莓中的网格上。 这是我的代码:

  Statement statement56 = db.createStatement("SELECT date,bill,rec,narration,id FROM Temp4");
  statement56.prepare();
  statement56.execute();

  Cursor c = statement56.getCursor();

  for (int i = 1; i < grid.getRowCount(); i++)
  {
      System.out.println("Inside first for loops");
      for (int j = 0; j < grid.getColumnCount() ; j++)
      {
          System.out.println("Inside second for loops");

          while(c.next()) 
          {

              System.out.println("Inside while"); 
              Row r;
              r = c.getRow();



              for (int k = 4; k >=0; k--)
              {
                  System.out.println("Inside for loops");
                  if(k==0 || k==3)
                  {
                      System.out.println("Retrieving date or narration");
                      grid.insert(new LabelField(r.getString(k)),i,j);
                  }  
                  else
                  {
                      System.out.println("Retrieving other values"); 
                      //  String p = r.getString(k)
                      String p = "" + r.getInteger(k);
                      grid.insert(new LabelField(p),i,j);
                  }                           
              } 

              // c.previous();
              //c--;
              System.out.println("Exiting while");                        
          }

          System.out.println("Exiting sec for");
          break;
      }
      System.out.println("Exiting first for");

      break;
   }

   statement56.close(); 
   db.close();

这以自下而上的方式为我提供了我的 sqlite 表的值。我希望它与插入到 sqlite 表中的方式完全相同。

即如果我的表包含

a b c d e
f g h i j
k l m n o
p q r s t
u v w x y

我在黑莓的网格布局中得到的输出是

u v w x y
p q r s t
k l m n o
f g h i j 
a b c d e

任何知道解决方案的人。期待合理的帮助。谢谢。

【问题讨论】:

    标签: sqlite blackberry gridview cursor


    【解决方案1】:

    只需在您的 SQL 查询中添加 ORDER BY

    SELECT date,bill,rec,narration,id FROM Temp4 ORDER BY id DESC
    

    如果更合适,您可以使用另一个排序字段而不是 id

    如果没有要排序的字段,可以使用伪字段ROWID。该字段可用作任何 SQLite 表中的自增字段:

    SELECT date,bill,rec,narration,id FROM Temp4 ORDER BY ROWID DESC
    

    【讨论】:

    • 感谢您的回复。但我的表 temp4 包含以随机方式插入的数据。另外,在我的情况下,id 列不是唯一的,因为它是从另一个表中插入的。所以我找到了很难定义一个特定的列用作 ORDER BY。有没有其他方法可以检索以随机顺序插入的值以进行随机提取?
    • 您应该创建另一个字段(我们称之为number),将其设为AUTO INCREMENT PRIMARY KEY 并用于排序。自动递增字段总是有用的,反正有它会更好。
    • 是的,我在其他表中使用了自动增量。但这是一个临时表,我没有使用它。无论哪种方式都为列提供编号。
    • 尝试使用ORDER BY ROWID DESCROWID 是可以在任何 SQLite 表中使用的伪列。类似于自增字段。
    • 非常感谢!!!这很有帮助。我从几个小时以来一直在努力解决这个问题。再次感谢。
    【解决方案2】:

    如果你有这样的行:

    Id Name
    1  Row1
    2  Row2
    3  Row3
    

    使用 ORDER BY CLAUSE 可以得到如下结果:

    Id Name
    3  Row3
    2  Row2
    1  Row1
    

    在您的情况下,您可以使用以下查询:

    SELECT date,bill,rec,narration,id FROM Temp4 ORDER BY id DESC
    

    【讨论】:

    • 感谢您的回复。但是我的表 temp4 包含以随机方式插入的数据。另外,在我的情况下,id 列不是唯一的,因为它是从另一个表中插入的。所以我找到了很难定义一个特定的列用作 ORDER BY。有没有其他方法可以检索以随机顺序插入的值以进行随机提取?
    • 您可以有一个像 DateCreated 这样的列,然后您可以在 ORDER BY CLAUSE 中使用该列。
    • 我的建议是使用单独的列“DateCreated”,以便您可以在 ORDER BY CLAUSE 中使用它..
    • 非常感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 2013-11-18
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    • 2021-10-01
    相关资源
    最近更新 更多