Selection arguments are automatically used as strings. Change this:
String[] whereArgs = new String[] {"'%" + mFilter + "%'" };
To that:
String[] whereArgs = new String[] {"%" + mFilter + "%" }; (removed ' from your strings)
With the additional ' there, it cancels the text and becomes ''%John%'' The builder automatically processes ' for the selection arguments.
EDIT
Modify your request as well:
String sql = "SELECT * FROM Salary WHERE emp_id in (select _id from Employee where employee_name like ?)"; Cursor c = getDB(mContext).rawQuery(sql, whereArgs);
EDIT 2
I recreated your setup with the class below, and all my code worked fine. I pulled John out of user and got both results. I believe that the problem is creating a database or you simply do not have data in your database. Use DDMS to pull out the database and open it using the SQLite browser. Check if there is any data in your database. If so, the table creation types do not match the select query. When I call GetMyValues (), I get 2 records returned from the cursor.
public class DataBaseHandler extends SQLiteOpenHelper { private static final String TAG = "DBHandler"; //Database VERSION private static final int DATABASE_VERSION = 2; //DATABASE NAME private static final String DATABASE_NAME = "test"; //DATABASE TABLES private static final String TABLE_SALARY = "Salary"; private static final String TABLE_EMP = "Employee"; //DATABASE FIELDS private static final String SalaryID= "_id"; private static final String SalaryEmpName = "employee_name"; private static final String EmpID= "_id"; private static final String EmpAmt = "amount"; private static final String EmpSalID = "emp_id"; //DATABASE TYPES private static final String INTPK = "INTEGER PRIMARY KEY"; private static final String INT = "INTEGER"; private static final String TEXT = "TEXT"; //CREATE TABLES private static final String CREATE_SALARY_TABLE = "CREATE TABLE " + TABLE_SALARY + "(" + EmpID + " " + INTPK + "," + EmpAmt + " " + INT + "," + EmpSalID + " " + INT + ")"; //CREATE TABLE Salary(_id INTEGER PRIMARY KEY,amount INTEGER,emp_id INTEGER) private static final String CREATE_EMPLOYEE_TABLE = "CREATE TABLE " + TABLE_EMP + "(" + SalaryID + " " + INTPK + "," + SalaryEmpName + " " + TEXT + ")"; //CREATE TABLE Employee(_id INTEGER PRIMARY KEY, employee_name TEXT) public DataBaseHandler(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_EMPLOYEE_TABLE); db.execSQL(CREATE_SALARY_TABLE); insertEmployeeValues(db); insertSalaryValues(db); } private void insertEmployeeValues(SQLiteDatabase db){ ContentValues values = new ContentValues(); values.put(SalaryEmpName, "John"); db.insert(TABLE_EMP, null, values); values.clear(); values.put(SalaryEmpName, "Rocky"); db.insert(TABLE_EMP, null, values); values.clear(); values.put(SalaryEmpName, "Marry"); db.insert(TABLE_EMP, null, values); values.clear(); } private void insertSalaryValues(SQLiteDatabase db){ ContentValues values = new ContentValues(); values.put(EmpAmt, 500); values.put(EmpSalID, 1); db.insert(TABLE_SALARY, null, values); values.clear(); values.put(EmpAmt, 400); values.put(EmpSalID, 1); db.insert(TABLE_SALARY, null, values); values.clear(); values.put(EmpAmt, 600); values.put(EmpSalID, 2); db.insert(TABLE_SALARY, null, values); values.clear(); values.put(EmpAmt, 700); values.put(EmpSalID, 2); db.insert(TABLE_SALARY, null, values); values.clear(); values.put(EmpAmt, 350); values.put(EmpSalID, 3); db.insert(TABLE_SALARY, null, values); values.clear(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMP); db.execSQL("DROP TABLE IF EXISTS " + TABLE_SALARY); onCreate(db); } public int GetMyValues(){ String mFilter = "John"; String[] whereArgs = new String[]{"%" + mFilter + "%"}; int count = 0; SQLiteDatabase db = this.getWritableDatabase(); String where = " emp_id in (select _id from Employee where employee_name like ? )"; Cursor c = db.query("Salary",null, where, whereArgs,null,null,null); count = c.getCount(); c.close(); return count; } }
Link to the project:
Can you turn on? s in the selection, which will be replaced by the values from selectionArgs so that they appear in the selection. values will be linked as strings