how to use like clause with db.query(....) in SQLite
hi i am new to android and got stuck in a problem
i have a table of two fields songname and songhits in my database .Now i want to get all songs starting with a string say MATCH_STR sorted by songhits in a list.I know the sql query but i dont know how to execute in my android app.pls help me with an example or correct code.
my code :
Cursor c = myDB.query(MY_DATABASE_TABLE, "songname","songname like %"+MATCH_STR+"%" , null, null,"SongHit", null);
if (c != null)
{
c.moveToFirst();
int SongNameColumn = c.getColumnIndex("SongName");
if (c.isFirst())
{
i = 0;
do {
i++;
String SongName = c.getString(SongNameColumn); 开发者_JS百科
// Add current Entry to results.
results.add(SongName);
} while (c.moveToNext());
}
}
Cursor c = myDB.query(MY_DATABASE_TABLE, "songname","songname like ?" , new String[]{"%"+MATCH_STRING+"%"}, null,"SongHit", null);
May be these will one hElp!!
i think you have missed single quote link like operator
Cursor c = myDB.query(MY_DATABASE_TABLE, "songname","songname like '%"+MATCH_STR+"%'" , null, null,"SongHit", null);
check and correct your query
try below code for search :-
public Cursor getROUTINENAME(String search_str) {
String query = "SELECT * FROM TBNAME where COL_NAME LIKE '%"+search_str+"%'";
Cursor mCursor = db.rawQuery(query, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
call above function with string .
精彩评论