开发者

Android SQLite rawquery returns only first record

My SQLite query returning only one record, However, the tab开发者_开发百科le has multiple rows

cursor=mydb.rawQuery("Select category from items;", null);

I have even tried GROUP BY but still wont work.

I am new to SQLite, would appreciate any help. Thanks.


First of all your string query must not be terminated so instead of passing it as:

"Select category from items;"

you should try passing it as:

"Select category from items"

as mentioned on this page.

Also, are you looping over the cursor? Here is an example of how to get data out of a cursor with a while loop:

ArrayList<String> results = new ArrayList<String>()
while (cursor.moveNext()) {
    results.add(cursor.getString(0)); // 0 is the first column 
}


First, search:

Cursor cs = myDataBase.rawQuery("Your query", null);
if (cs.moveToFirst()) {
    String a = cs.getString(cs.getColumnIndex("your_column_name"));
    cs.close();
    return a;
}
cs.close();
return "";

Get the information from cursor:

if (cs.moveToFirst()) {
    ArrayList<String> results = new ArrayList<String>()
    do {
            results.add(cs.getString(cs.getColumnIndex("your_column_name"))); 
    } while (cs.moveNext());
}

Without if, I took error in my project. But this worked for me. By the way, your query doesn't look good. If you give some information about your database, we can help much more.


Use this to select all items from the table:

Cursor cursor = db.rawQuery("SELECT * FROM Tablename , null);


this can help you

public ArrayList<mydata> getallContents() {

    ArrayList<mydata> lst = new ArrayList<mydata>();

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor c = db.rawQuery("select * from " + GAMETABLE, null);

    if (c.moveToFirst()) {
        do {
            lst.add(new mydata(c.getString(1),
                    c.getString(3),c.getString(4),c.getString(5),c.getString(6)));

        } while (c.moveToNext());
    }
    db.close();
    return lst;
}


you don't need raw query method. i think that the android way is better in this case:

    db.query(items, category, null, null, null, null, null);

than use the cursor how already is written in the other comment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜