Android sqlite: how to get specific data from a specific column?

I am developing a restaurant menu application. My application has a sqlite table that has the following columns:

"id", "category", "item_name"

The content of a category column has a type string. The primary key of the table id.

I want to receive data of a certain category.

For example, I want to get the element names of all the elements that belong to a category Veg, and then display this result in a list.

I tried different requests, but both of them do not work. Please help me.

String vg ="Veg";
Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
           KEY_ROWID, KEY_CATEGORY, KEY_NAME,KEY_PRIZE,KEY_DESCRIPTION },
        KEY_CATEGORY + "=" + vg , null, null, null,null, null);
        if (mCursor != null) {              
                        mCursor.moveToFirst();
        }
        return mCursor;

Raw request

String query = "SELECT * FROM todo WHERE category =" + vg ;
          Cursor  cursor = database.rawQuery(query,null);
            if (cursor != null) {
                cursor.moveToFirst();
            }
            return cursor;
+5
source share
7 answers

try the following:

String query = "SELECT * FROM todo WHERE category='" + vg;

Cursor  cursor = database.rawQuery(query,null);

        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
+14
source
String query = "SELECT item_name FROM todo WHERE category =" + vg ;
Cursor  cursor = database.rawQuery(query,null);
if (cursor.moveToFirst()) {
         while (cursor.isAfterLast() != true) {
           string itemname =  cursor.getString(cursor.getColumnIndex("item_name")));
          }
}

moveToFirst , , , , while. adaper . , .

+9
dbHelper = new DBHelper(getApplicationContext());
SQLiteDatabase db = dbHelper.getReadableDatabase();

Cursor cursor = db.rawQuery("select * from centuaryTbl where email='"+email+"'",null);
if (cursor.moveToFirst())
{
    do
    {
        String s1 = cursor.getString(cursor.getColumnIndex("s1"));
        String s2 = cursor.getString(cursor.getColumnIndex("s2"));
        String s3 = cursor.getString(cursor.getColumnIndex("s3"));


    }while (cursor.moveToNext());
}
+5
String query = "SELECT * FROM Table_Name WHERE Col_Name='name'";
Cursor cursor = mDb.rawQuery(query, null);
+2

:

String vg ="Veg";
return database.rawQuery("SELECT * FROM subjects where " + KEY_CATEGORY + " = ?",
                            new String[]{vg});
+1
  String q="SELECT * FROM todo WHERE category='" + vg +"'";
    SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = null;
        cursor = db.rawQuery(q, null);
        if (cursor.moveToFirst()) {
            do {

            } while (cursor.moveToNext());

        }
0
String selectQuery = "select * from " + TABLE;
        sqlDatabase = this.getWritableDatabase();

     Cursor cursor = sqlDatabase.rawQuery(selectQuery, null);

    if (cursor != null) {
        cursor.moveToFirst();
    }


    return cursor;

..

0

All Articles