Title: How to search a specific string in database in sqlite
Tags: java;android;sqlite;search
Question: I wanted to search for specific keyword existing in database and show them out.
Here's my code for search class:
String s = kw.getText().toString();
sqlhandler show = new sqlhandler(this);
show.open();
String rname = show.getName(s);
show.close();
tv.setText(rname);
And here's my database class:
public String getName(String kw) {
String[] columns = new String[] {NAME, APRICE, DPRICE, VENDOR, KEYWORD };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, "DPRICE like " + "'%"+ kw+"%'", null, null, null, null);
String sresult = "";
int iname = c.getColumnIndex(NAME);
int iap = c.getColumnIndex(APRICE);
int idp = c.getColumnIndex(DPRICE);
int ivendor = c.getColumnIndex(VENDOR);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
sresult = sresult + c.getString(iname)+ " "+c.getString(iap)
+ "  " + c.getString(idp) + "  " + c.getString(ivendor)
+ "\n";
return sresult;
What is my problem here? I keep getting force close everytime I run it.
Comment: Post full exception stack.


Here is another answer: Since you haven't posted your LogCat I can only guess, but your WHERE clause looks suspicious:
"DPRICE like ..."
I assume DPRICE is a variable, not the actual column name:
DPRICE + " like ..."
Also I have a few recommendations:
Use the selectionArgs parameter to prevent SQL injection attacks.
Consider using a ListView and SimpleCursorAdapter to display your data.
Only select the columns that you are using.
Use a while-loop instead of a for-loop to iterate through your Cursor (your for-loop is doing three times the necessary work).
All together:
public String getName(String kw) {
String[] columns = new String[] {NAME, APRICE, DPRICE, VENDOR};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, DPRICE + " like ?", new String[] {"'%" + kw + "%'"}, null, null, null, null);
int iname = c.getColumnIndex(NAME);
int iap = c.getColumnIndex(APRICE);
int idp = c.getColumnIndex(DPRICE);
int ivendor = c.getColumnIndex(VENDOR);
// moveToNext() returns true when there is more data to read
//     or false when the cursor isAfterLast()
StringBuilder builder = new StringBuilder();
while (c.moveToNext()) {
builder.append(c.getString(iname).append(" ")
.append(c.getString(iap).append("  ")
.append(c.getString(idp).append("  ")
.append(c.getString(ivendor).append("\n");
return builder.toString();
Here is a tutorial on how to use a ListView with a custom layout to display information from a database.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:08/31/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><android><sqlite><search>', 'Title': 'How to search a specific string in database in sqlite', 'ViewCount': 1272, 'id': 12217862, 'user_AboutMe': None, 'user_AccountId': 1800481.0, 'user_DisplayName': 'Marco', 'user_Reputation': 1.0, 'user_Views': 0.0, 'user_id': 1638969.0}}