游标 while 循环返回除最后一个值之外的所有值

发布于 2024-08-28 05:26:52 字数 1528 浏览 7 评论 0原文

我使用 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.

谢谢。

I am using a while loop to iterate through a cursor and then outputing the longitude and latitude values of every point within the database.

For some reason it is not returning the last (or first depending on if I use Cursor.MoveToLast) set of longitude and latitude values in the cursor.

Here is my code:

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);
    }
}

From this I am getting:

*******************************************
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.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云胡 2024-09-04 05:26:52

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());
}

moveToNext() has two features. It returns a boolean signifying that there is a next, but at the same time it goes ahead and moves the cursor.

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());
}
时光清浅 2024-09-04 05:26:52
Cursor c=null;
c=......;
try {
    if (c!=null) {
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

        }

    }
} finally {
    if (c!=null) {
        c.close();
    }
}
Cursor c=null;
c=......;
try {
    if (c!=null) {
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

        }

    }
} finally {
    if (c!=null) {
        c.close();
    }
}
倾城泪 2024-09-04 05:26:52

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

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

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

我会将您的 while 循环转换为 do-while 循环

Your skipping the first value actually, not the last.

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

The first time into the while loop you are pointing at the 2nd row.

I would convert your while loop to a do-while loop

童话里做英雄 2024-09-04 05:26:52

您刚刚执行了查询。你不能去掉 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);
    }
}

You just executed the query. Can't you just get rid of the 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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文