args di selezione vengono utilizzati automaticamente come stringhe. Modificare questa:
String[] whereArgs = new String[] {"'%" + mFilter + "%'" };
a questo:
String[] whereArgs = new String[] {"%" + mFilter + "%" }; (removed ' from your strings)
Con in più '
in là si annulla il testo e diventa ''%John%''
Il costruttore gestisce automaticamente '
per i args di selezione.
EDIT
modificare la tua ricerca anche a questo:
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
Ho ricreato la configurazione con la classe di sotto e tutto il mio codice eseguito bene. Ho estratto dall'utente John e ho ottenuto entrambi i risultati. Credo che il problema sia nella creazione del tuo database o semplicemente non hai dati all'interno del tuo database. Usa DDMS per estrarre il database e aprirlo con SQLite Browser. Controlla se il tuo database contiene dati al suo interno. Se lo fa, i tipi di creazione della tabella non corrispondono all'istruzione select. Quando chiamo GetMyValues () ottengo 2 record restituiti dal cursore.
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;
}
}
Dev Reference:
è possibile includere? S in selezione, che saranno sostituiti da valori da selectionArgs, in modo che essi appaiono nella selezione. I valori saranno legati da stringhe
Ho provato a rimuovere il 'dal codice .. ma non ha funzionato neanche. – mudit
@mudit Puoi provare cosa c'è nella mia sezione di modifica per vedere se funziona – ObieMD5
ho provato a usare il 2 modo .. sta funzionando sul client sqlite di firefox ma non funziona su Android. – mudit