【问题标题】:Sqllite query returns cursor with 0 rowsSqlite 查询返回 0 行的游标
【发布时间】:2012-08-16 02:54:23
【问题描述】:

我正在尝试使用之前填充的 sqlite 数据库中的查询来检索数据。但由于某种原因,查询 returnscursor 没有行。

所以这是我尝试检索数据的部分:

        database.open();
    Cursor cursor = database.getComponentData("CONTROLS", null, null);

    int test = cursor.getCount();//This is the part where i test through the eclipse debugger whether there is  any rows return
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {

        get.append("Video controls : "+cursor.getString(1)+" Audio Controls : "+cursor.getString(2)+" Reader controls : "+cursor.getString(3));
        cursor.moveToNext();
    }
    cursor.close();
    Cursor cursor1 = database.getComponentData("LIST", new String[]{"'URL'"},"AUDIO");

    get.append("Video list : ");
        cursor1.moveToFirst();
    while (!cursor1.isAfterLast()) {

        get.append(" "+cursor1.getString(1)+" "+cursor1.getString(2)+" "+cursor1.getString(3));
        cursor1.moveToNext();

    }
    cursor1.close();
    database.close();

这是我查询的数据库适配器中的部分。

public Cursor getComponentData(String whichTable,String[] whichColumn,String whichSection) {
    Cursor c = null;
    if(whichSection!=null) 
    {   
        return database.query(whichTable, whichColumn,"SECTION = '"+whichSection+"'",null, null, null, null);
    }
    else{
        return database.query(whichTable, whichColumn,null,null, null, null, null);
    }

}

这是我之前插入数据库的部分:

                 database.open();
               ContentValues control = new ContentValues();
               control.put("VIDEO_CONTROLS",video_control);
               control.put("AUDIO_CONTROLS",audio_control);
               control.put("READER_CONTROLS",book_control);

               control_long =database.insertNewControl(control);

               ContentValues temp_list = new ContentValues();

                a_p=audio_playlist.split(",");
                v_p=video_playlist.split(",");
                b_p=booklist.split(",");
               for(int i =0;i<a_p.length;i++){
                   temp_list.put("NAME", "test");
                   temp_list.put("URL", a_p[i]);
                   temp_list.put("SECTION", "AUDIO");

                list_long=   database.insertNewListRow(temp_list);

               }
               for(int i =0;i<v_p.length;i++){
                  temp_list.put("NAME", "test");
                   temp_list.put("URL", v_p[i]);
                   temp_list.put("SECTION", "VIDEO");
                   list_long=    database.insertNewListRow(temp_list);

                   }
               for(int i =0;i<b_p.length;i++){
                 temp_list.put("NAME", "test");
                   temp_list.put("URL", b_p[i]);
                   temp_list.put("SECTION", "READER");
                   list_long=   database.insertNewListRow(temp_list);

                   }
                database.close();

在适配器中插入方法:

    public Long insertNewControl(ContentValues controls_values) {
return database.insert("CONTROLS", null, controls_values);  
}
public Long insertNewListRow(ContentValues list_values){
return database.insert("LIST", null, list_values);  
}

感谢阅读。

编辑: 忘了提这一点,如果我在插入这些行后立即从数据库中query(),我将能够取出行。但是我要在close()open() 之后再次查询这些行, 游标不返回任何行。

【问题讨论】:

  • 您是否确认数据在数据库中(BASICS)?
  • 如insert()是否返回非“-1”值?
  • 或者在eclipse中使用sqlite工具检查数据库。
  • 虽然我还没有使用 sqlite 工具但是对于 insert() 返回的内容,上次我检查它没有返回错误(-1)
  • 请问我在哪里可以找到这样一个sqlite工具来检查我的数据库?

标签: java android sqlite cursor


【解决方案1】:

删除了我关于 cursor 的回答(第一个光标)

cursor1 return null 因为你不需要在列名之间放',只需放"URL"而不是"'URL'"

你也不需要测试计数,moveToFirst() 本身返回一个布尔值,如果游标为空,它将返回 false。所以你需要做的是,

if (cursor.moveToFirst()) {
    //do what you want
}

【讨论】:

  • 关于 whichColumn :传递 null 是有效的,但它只是不鼓励
  • 哦,是的,我的错,我认为它会返回 null,会改变我的答案。
  • 即使删除撇号它仍然没有返回任何行但感谢您的回复。
【解决方案2】:

我忘记发布我找到的解决方案,

基本上发生的事情是我忘记在创建数据库和表的类的构造函数中命名数据库。 epicfacepalm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2017-08-11
    相关资源
    最近更新 更多