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

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

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; 

回答1:

try this:

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


回答2:

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

Here moveToFirst checks if there are items satisfying the criteria and then loops through cursor using while loop. The string itemname can be replaced by list adaper to populate the data. Hope this helps.



回答3:

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


回答4:

String query = "SELECT * FROM Table_Name WHERE Col_Name='name'"; Cursor cursor = mDb.rawQuery(query, null); 


回答5:

Try this:

String vg ="Veg"; return database.rawQuery("SELECT * FROM subjects where " + KEY_CATEGORY + " = ?",                             new String[]{vg}); 


回答6:

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


回答7:

String selectQuery = "select * from " + TABLE;         sqlDatabase = this.getWritableDatabase();       Cursor cursor = sqlDatabase.rawQuery(selectQuery, null);      if (cursor != null) {         cursor.moveToFirst();     }       return cursor; 

this will help you..



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!