简体   繁体   中英

android where clause query not working

String name="xyz";    
String sql="select * from "+table_name+" where person_name='"+name+"'";

Above query is working fine. but if I replace person_name with any other column name like person_item or person_trip, no results are shown and there is no error.

String sql="select * from "+table_name+" where person_item='"+name+"'";

This query doesn't work. What can be the possible error? I have been trying to get this work for last 7 days. Please help, thanks in advance.

You could try this - using a cursor

Where myDB is an instance of SQLiteDatabase...

Cursor c = myDB.rawQuery("SELECT * FROM " + table_name , null);

int ITEM_COLUMN = c.getColumnIndex("person_item");

myDB.execSQL("SELECT * FROM "+table_name+" WHERE "+c.getColumnName(ITEM_COLUMN)+" = "+name+";");

You could read more about cursors and their interaction with databases here: http://developer.android.com/reference/android/database/sqlite/SQLiteCursor.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM