简体   繁体   English

Android SQL查询无法识别第一个条目

[英]Android SQL query not recognizing first entry

Buckle up folks, this is a weird one. 扣好乡亲,这是一个奇怪的事情。 I'm currently working on an android app that involves storing and retrieving data in an sqlite database. 我目前正在开发一个涉及在sqlite数据库中存储和检索数据的android应用。 I was going through the app and testing some of the basic features to make sure everything worked, and lo and behold I found a bug in retrieving data from my database. 我正在浏览该应用程序,并测试了一些基本功能以确保一切正常,然后发现我从数据库中检索数据时发现了一个错误。 When a user inputs their very first entry to the app, everything works as expected, the values get processed and stored. 当用户将他们的第一个条目输入应用程序时,一切都会按预期进行,值会被处理和存储。 However, when I go back and attempt to access that data using SELECT * FROM history; 但是,当我返回并尝试使用SELECT * FROM history;访问该数据时SELECT * FROM history; I get a cursor that returns true when I call data.moveToNext() , yet when I loop through it using while(data.moveToNext()) { //get values and add to a List<> } the while loop never gets executed. 当我调用data.moveToNext() ,我得到一个返回true的游标,但是当我使用while(data.moveToNext()) { //get values and add to a List<> }遍历它while(data.moveToNext()) { //get values and add to a List<> } while循环永远不会执行。

I've looked at the contents of the database after moving the file to my computer and opening the database using this db browser and I can see my entry. 将文件移至计算机并使用数据库浏览器打开数据库后,我查看了数据库的内容,我可以看到我的输入。

Here's the method that I call to get all the points from my database: 这是我从数据库中获取所有点的方法:

List<PointValue> getAllPoints() {
    List<PointValue> points;
    Cursor data = rawQuery("SELECT * FROM history");
    if (data.moveToNext()) {
        points = new ArrayList<>();
        while (data.moveToNext()) {
            System.out.println("Looped");
            long timestamp = data.getLong(data.getColumnIndexOrThrow("timestamp"));
            int level = data.getInt(data.getColumnIndexOrThrow("level"));
            points.add(new PointValue(timestamp, level));
        }
    } else {
        return null;
    }
    data.close();
    if (points.size() == 0) {
        return null;
    }
    return points;
}

The rawQuery method looks like this: rawQuery方法如下所示:

private Cursor rawQuery(String sql) {
    SQLiteDatabase db = this.getReadableDatabase();
    return db.rawQuery(sql, null);
}

When I tried debugging this on my own, the size of points is 0 even though I know that there's at least one point in the database. 当我尝试自行调试时,即使我知道数据库中至少有一个点, points的大小也为0。 Thoughts? 思考? The class containing all of my sql related stuff extends SQLiteOpenHelper 包含我所有与SQL相关的东西的类扩展了SQLiteOpenHelper

EDIT: Here's the solution suggested by @Isaac Payne (still doesn't work): 编辑:这是@Isaac Payne建议的解决方案(仍然不起作用):

public List<PointValue> getAllPoints() {
    List<PointValue> points = new ArrayList<>();
    Cursor data = rawQuery("SELECT * FROM history");

    while (data.moveToNext()) {
        long timestamp = data.getLong(data.getColumnIndexOrThrow("timestamp"));
        int level = data.getInt(data.getColumnIndexOrThrow("level"));
        points.add(new PointValue(timestamp, level));
    }
    data.close();
    if (points.size() == 0) {
        return null;
    }
    return points;
}

The issue is that when you call data.moveToNext() in the if statement you are moving to the first entry, then you call moveToNext() again in your while loop moving to the second non-existent entry. 问题是,当您在if语句中调用data.moveToNext() ,您将移至第一个条目,然后在while循环中再次调用moveToNext() ,移至第二个不存在的条目。 Try removing the if statement 尝试删除if语句

Add data.moveToFirst() before if loop. 在if循环之前添加data.moveToFirst()。

Cursor data = rawQuery("SELECT * FROM history");
//add this line
data.moveToFirst();
if (data.moveToNext()) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM