Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

like operator syntax in sqlite with android

Tags:

android

I'm developing with Android and I need to do a query with sqlite using the like operator and variables.

The piece of code I just try to modify is, the implementation of a database present at this link.

So, I'm trying to do a query on the table created in the link in order to select just a name from a table. I'm doing like this:

Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " +
SAMPLE_TABLE_NAME + " where" +field+ " like %"+search+"%" , null);

where field and search are two variable defined above in this way:

final String field = "LastName";
final String search = "Makam";

If I execute the app in output, I should see a row with the name and age that I have selected in the query.But, I obtain nothing!!!

The Logcat of Eclipse shows :

sqlite returned: error code = 1,msg = near "like": syntax error.

But I'm pretty sure the syntax it' s correct. Could someone help me?

like image 952
user2312945 Avatar asked May 02 '13 10:05

user2312945


3 Answers

use this way:

Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " +
SAMPLE_TABLE_NAME + " where " +field+ " like '%"+search+"%'" , null);

Edited: create function

public Cursor getSearch(String SAMPLE_TABLE_NAME, String field,
            String search) {
        SQLiteDatabase sampleDB = this.getReadableDatabase();
        Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM "
                + SAMPLE_TABLE_NAME + " where " + field + " like '%" + search
                + "%'", null);
        return c;
    }
like image 114
Dhaval Parmar Avatar answered Nov 12 '22 12:11

Dhaval Parmar


Looks to me as though you might be missing a space between WHERE and field.

like image 39
nurdglaw Avatar answered Nov 12 '22 11:11

nurdglaw


selection.append(column_name).append(" like ?");
                    selectionArgs = new String[]{"%" + mCategoryName + "%"};
                    db.query(TABLE_URI,null,selection.toString(),selectionArgs,null);
like image 3
Kona Suresh Avatar answered Nov 12 '22 10:11

Kona Suresh