简体   繁体   English

Android Studio-SQLite光标不返回任何数据

[英]Android Studio - SQLite Cursor not returning any data

I am trying to return the names of distinct values in the 'category' column of an SQLite database. 我试图在SQLite数据库的“类别”列中返回不同值的名称。 The cursor does not appear to be returning any results. 光标似乎未返回任何结果。

I'm building an Android app using a pre-existing database of bird species. 我正在使用现有的鸟类数据库构建一个Android应用。 The aim is to allow the user to explore the database through the app. 目的是允许用户通过该应用程序浏览数据库。 The problem I'm having is in trying to return the distinct categories of bird species that exist in the database. 我遇到的问题是试图返回数据库中存在的不同种类的鸟类。 The database appears to be opening successfully - no SQLite exception is being thrown, - but after using the query, the '.moveToNext' method does not appear to be returning any data. 数据库似乎已成功打开-没有引发SQLite异常-但是在使用查询后,“。moveToNext”方法似乎未返回任何数据。

public ArrayList<String> getCategory(String[] name){
        String TABLE_BIRDS = "main";
        String[] COLUMN = name;
        ArrayList<String>categories = new ArrayList<>();

        if (name[0]!=null)
        {
            Log.d(LOG_TAG, name[0]);
        } else {
            Log.d(LOG_TAG, "name[0] has not been passed");
        }
        x = db.query(true, TABLE_BIRDS, new String[] { COLUMN[0] } , null, null, COLUMN[0], null, null, null );
        if (x.moveToNext()) {
            Log.i(LOG_TAG, x.getString(x.getColumnIndex("category")));
        }
        else {
            Log.i(LOG_TAG, "The cursor is not returning any data");
        }
        //Simple cursor loop
        if (x.moveToNext()){
            String category = new String();
            category = x.getString(x.getColumnIndex(category));
            categories.add(category);
            Log.i("cursor loop", category);
        }
        return categories;

In the above code, the log messages are showing that: getCategory is receiving the expected string "category" before the query, but right after the query, the if/else loop is reporting that "The cursor is not returning any data". 在上面的代码中,日志消息显示:getCategory在查询之前接收到预期的字符串“ category”,但是在查询之后,if / else循环正在报告“光标未返回任何数据”。

What I expected is that the query would return six Strings, the cursor loop would add them to the ArrayList 'categories', and this ArrayList would be returned. 我期望查询将返回六个字符串,光标循环会将它们添加到ArrayList'categories'中,并将返回此ArrayList。

Please any help would be appreciated. 请任何帮助将不胜感激。

Assuming the cursor is not null, you should iterate over the results with a while/for loop. 假设光标不为空,则应使用while / for循环遍历结果。

eg : while(x.moveToNext()) { //your logic } 例如: while(x.moveToNext()) { //your logic }

It is always useful when debugging this kind of issues to install an SQLite DB browser then check the cursor's query against your DB to see if you have any data. 在调试此类问题时,安装SQLite数据库浏览器然后针对您的数据库检查游标查询以查看是否有任何数据总是很有用的。

Here is a simpler version of your method: 这是您的方法的简单版本:

public ArrayList<String> getCategory(String[] name) {
    String TABLE_BIRDS = "main";
    ArrayList<String> categories = new ArrayList<>();

    if (name[0] != null) {
        Log.d(LOG_TAG, name[0]);
    } else {
        Log.d(LOG_TAG, "name[0] has not been passed");
    }

    Cursor x = db.query(true, TABLE_BIRDS, new String[]{name[0]}, null, null, null, null, null, null);

    while (x.moveToNext()) {
        String category = x.getString(0);
        categories.add(category);
        Log.i("cursor loop", category);
    }

    if (x.getCount() == 0) {
        Log.i(LOG_TAG, "The cursor is not returning any data");
    }
    x.close();

    return categories;
}

I guess name[0] contains the string 'category' which is the column that you want to query. 我想name[0]包含字符串'category' ,这是您要查询的列。
Since you pass true as the 1st argument in the method query() this means that you will get distinct values, so no need for the group by argument. 由于在方法query()中将true作为第一个参数传递,这意味着您将获得不同的值,因此不需要group by参数。
You don't need the variable COLUMN since you have what you need in the variable name . 您不需要变量COLUMN,因为变量name
Also you don't need getColumnIndex() since the cursor returns only 1 column. 另外,由于游标仅返回1列,因此您不需要getColumnIndex()
Edit 编辑
Instead of: 代替:

Cursor x = db.query(true, TABLE_BIRDS, new String[]{name[0]}, null, null, null, null, null, null);

try rawQuery(): 尝试rawQuery():

Cursor x = db.rawQuery("SELECT DISTINCT " + name[0] + " FROM " + TABLE_BIRDS, null);

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

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