【问题标题】:Cursor while loop returning every value but the last游标 while 循环返回除最后一个值之外的所有值
【发布时间】:2010-04-02 15:49:44
【问题描述】:

我正在使用 while 循环遍历游标,然后输出数据库中每个点的经度和纬度值。

由于某种原因,它没有返回游标中最后一个(或第一个取决于我是否使用 Cursor.MoveToLast)经度和纬度值集。

这是我的代码:

public void loadTrack() {
SQLiteDatabase db1 = waypoints.getWritableDatabase();
Cursor trackCursor = db1.query(TABLE_NAME, FROM, "trackidfk=1", null, null, null,ORDER_BY); 

    trackCursor.moveToFirst();
    while (trackCursor.moveToNext()) {
        Double lat = trackCursor.getDouble(2);
        Double lon = trackCursor.getDouble(1);
        //overlay.addGeoPoint( new GeoPoint( (int)(lat*1E6),  (int)(lon*1E6)));
        System.out.println(lon);
        System.out.println(lat);
    }
}

从这里我得到:

*******************************************
04-02 15:39:07.416: INFO/System.out(10551): 3.0
04-02 15:39:07.416: INFO/System.out(10551): 5.0
04-02 15:39:07.416: INFO/System.out(10551): 4.0
04-02 15:39:07.416: INFO/System.out(10551): 5.0
04-02 15:39:07.416: INFO/System.out(10551): 5.0
04-02 15:39:07.416: INFO/System.out(10551): 5.0
04-02 15:39:07.416: INFO/System.out(10551): 4.0
04-02 15:39:07.416: INFO/System.out(10551): 4.0
04-02 15:39:07.416: INFO/System.out(10551): 3.0
04-02 15:39:07.416: INFO/System.out(10551): 3.0
04-02 15:39:07.416: INFO/System.out(10551): 2.0
04-02 15:39:07.416: INFO/System.out(10551): 2.0
04-02 15:39:07.493: INFO/System.out(10551): 1.0
04-02 15:39:07.493: INFO/System.out(10551): 1.0
***************************************************************
7 Sets of values, where I should be getting 8 sets.

谢谢。

【问题讨论】:

    标签: java android sql sqlite android-cursor


    【解决方案1】:

    moveToNext() 有两个特点。它返回一个布尔值,表示有 下一个,但同时它继续前进并移动光标。

    public void loadTrack() {
    SQLiteDatabase db1 = waypoints.getWritableDatabase();
    Cursor trackCursor = db1.query(TABLE_NAME, FROM, "trackidfk=1", null, null, null,ORDER_BY); 
    
        trackCursor.moveToFirst();
        do {
            Double lat = trackCursor.getDouble(2);
            Double lon = trackCursor.getDouble(1);
            //overlay.addGeoPoint( new GeoPoint( (int)(lat*1E6),  (int)(lon*1E6)));
            System.out.println(lon);
            System.out.println(lat);
        } while (trackCursor.moveToNext());
    }
    

    【讨论】:

    • 你可能想要一些东西来确保有第一个元素,但如果它最终变成一个空元素,那可能不会太顺利。
    【解决方案2】:
    Cursor c=null;
    c=......;
    try {
        if (c!=null) {
            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
    
            }
    
        }
    } finally {
        if (c!=null) {
            c.close();
        }
    }
    

    【讨论】:

      【解决方案3】:

      您实际上跳过了第一个值,而不是最后一个。

      trackCursor.moveToFirst();
      while (trackCursor.moveToNext()) {
      

      第一次进入 while 循环时,您指向的是第二行。

      我会转换你的while loop to a do-while loop

      【讨论】:

      • 我尝试了 do while,但由于返回的行数可以是任意数字,我不确定 while 表达式应该是什么?
      • do {...} while (trackCursor.moveToNext()) 和你当前使用的while表达式一样。
      【解决方案4】:

      您刚刚执行了查询。你不能摆脱 moveToFirst() 吗?

      public void loadTrack() {
      SQLiteDatabase db1 = waypoints.getWritableDatabase();
      Cursor trackCursor = db1.query(TABLE_NAME, FROM, "trackidfk=1", null, null, null,ORDER_BY); 
      
          while (trackCursor.moveToNext()) {
              Double lat = trackCursor.getDouble(2);
              Double lon = trackCursor.getDouble(1);
              //overlay.addGeoPoint( new GeoPoint( (int)(lat*1E6),  (int)(lon*1E6)));
              System.out.println(lon);
              System.out.println(lat);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-03
        • 2018-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-29
        相关资源
        最近更新 更多