Use of IN keyword in android sqlite -


i have 2 tables:

1) employee -- _id, employee_name. 2) salary -- _id, amount, emp_id. 

sample data:

employee: 1 john 2 rocky 3 marry  salary:  1 500 1 //salary john 2 400 1 //salary john 3 600 2 //salary rocky 4 700 2 //salary rocky 5 350 3 //salary marry 

now, want search in salary table view whom have paid salary. let if search 'john' in salary table, should return row 1 , 2 john.

here trying:

string = " emp_id in (select _id employee employee_name ? )";  string[] whereargs = new string[] {"'%" + mfilter + "%'" };  cursor c = getdb(mcontext).query("salary", null, where, whereargs,                 null, null, null); 

but returning no results. please help.

update

i debugged code, , found following query executing in cursor:

select * salary emp_id in (select _id employee employee_name ? ); 

selection args automatically used strings. change this:

string[] whereargs = new string[] {"'%" + mfilter + "%'" }; 

to this:

string[] whereargs = new string[] {"%" + mfilter + "%" }; (removed ' strings) 

with ' in there cancels out text , becomes ''%john%'' builder automatically handles ' selection args.

edit
change query this:

string sql = "select * salary emp_id in (select _id employee employee_name ?)"; cursor c = getdb(mcontext).rawquery(sql, whereargs); 

edit 2

i recreated setup class below , code ran fine. pulled user john , got both results. believe problem within database creation or not have data within database. use ddms pull database , open sqlite browser. check see if database has data within it. if table creation types not match select statement. when call getmyvalues() 2 records returned 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 = " emp_id in (select _id employee employee_name ? )";         cursor c = db.query("salary",null, where, whereargs,null,null,null);         count = c.getcount();         c.close();         return count;     } } 

dev reference:

you may include ?s in selection, replaced values selectionargs, in order appear in selection. values bound strings


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -