2012-08-16 181 views
0

我试图检索使用查询从SQLite数据库以前因为某些原因没有行populated..but查询returnscursor数据..Sqllite查询返回光标0行

因此,这里的部分,其中我试图检索数据:

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

    int test = cursor.getCount();//This is the part where i test throught the eclipse debugger whelther 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); 
    } 

} 

这是其中i插入到数据库中先前的部分:

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

感谢阅读。

编辑: 忘记提到这一点,如果我是从数据库中查询()我已插入这些行之后,我将能够得到行out.But我是来查询这些行后我关闭()并再次打开(),游标不返回任何行。

+0

您确认数据是否在数据库中(** BASICS **)? – JoxTraex 2012-08-16 03:15:31

+0

与insert()是否返回非“-1”值一样? – user1602020 2012-08-16 03:32:10

+0

或使用eclipse中的sqlite工具检查数据库。 – JoxTraex 2012-08-16 03:32:53

回答

0

我忘了后,我找到了解决办法,

基本上什么发生的是,我忘了名字在类的构造函数的数据库中,数据库和表已创建。 epicfacepalm

1

删除我的回答回合cursor(第一光标)

光标1返回null,因为你不需要把'在列名之间,只是把"URL"代替"'URL'"

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

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

关于whichColumn:传递null是有效的,但它只是不鼓励 – SteveR 2012-08-16 03:43:45

+0

噢,我的坏我认为它会返回null,将改变我的答案。 – Aprian 2012-08-16 03:49:29

+0

即使删除撇号它仍然没有返回行,但感谢回复。 – user1602020 2012-08-16 04:31:49