Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android sqlite: how to retrieve specific data from particular column?

Tags:

android

sqlite

I am developing restaurant menu application. My app has a sqlite table which has these columns:

"id", "category", "item_name"

Contents of category column is of type string. Primary key of table is id.

I want to retrieve data of particular category.

For example, I want to retrieve item names of all items which are in category Veg and then display this result in list view.

I have tried following different queries but both are not working. 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 query

String query = "SELECT * FROM todo WHERE category =" + vg ;
          Cursor  cursor = database.rawQuery(query,null);
            if (cursor != null) {
                cursor.moveToFirst();
            }
            return cursor;
like image 369
sagar Avatar asked Sep 12 '11 11:09

sagar


People also ask

How would you retrieve data from SQLite table?

We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data. This method return the total number of columns of the table.

What does SQLite select return?

SQLite SELECT statement is used to fetch the data from a SQLite database table which returns data in the form of a result table. These result tables are also called result sets.


1 Answers

try this:

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

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

        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
like image 157
Lavanya Avatar answered Oct 15 '22 22:10

Lavanya