10

My app has an SQLite table with columns id, category and item_name. category is of type string. Primary key is id. I want to retrieve names of all items in category Veg then display this in list view. I tried following queries :

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; 

Query:

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

10 Answers 10

18

try this:

String query = "SELECT * FROM todo WHERE category='" + vg; Cursor cursor = database.rawQuery(query,null); if (cursor != null) { cursor.moveToFirst(); } return cursor; 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this also working. ' was missing after "=" from my query.
14
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.

Comments

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

Comments

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

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
1

Try this:

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

Comments

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

Comments

0
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..

Comments

0
public List<String> getAllData(String email) { db = this.getReadableDatabase(); String[] projection = {email}; List<String> list = new ArrayList<>(); Cursor cursor = db.query(Table_name,null,"email=?",projection,null,null,null,null); // cursor.moveToFirst(); if(cursor.moveToFirst()) { do { list.add(cursor.getString(cursor.getColumnIndex("id"))); list.add(cursor.getString(cursor.getColumnIndex("name"))); list.add(cursor.getString(cursor.getColumnIndex("email"))); list.add(cursor.getString(cursor.getColumnIndex("password"))); // cursor.moveToNext(); } while (cursor.moveToNext()); } return list; } 

Comments

0

here you can pass value for filter on

 public ArrayList<PlayListModel> getPlaylist(String playlistName) { SQLiteDatabase db = this.getReadableDatabase(); ArrayList<PlayListModel> obj = new ArrayList<PlayListModel>(); Cursor cursor = db.query(TABLE_PLAYLIST, new String[] { KEY_ID, KEY_TITLE, KEY_SINGER , KEY_PATH , KEY_PLAYLISTNAME , KEY_DUR }, KEY_PLAYLISTNAME+ "=?", new String[] { playlistName }, null, null, null, null); if (cursor != null) cursor.moveToFirst(); PlayListModel contact = new PlayListModel (cursor.getString(1), cursor.getString(2),cursor.getString(3), cursor.getString(4),cursor.getString(5)); obj.add(contact); // return contact return obj; } 

Comments

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.