选择参数自动用作字符串。改变这个:
String[] whereArgs = new String[] {"'%" + mFilter + "%'" };
对此:
String[] whereArgs = new String[] {"%" + mFilter + "%" }; (removed ' from your strings)
有了额外'
的内容,它会取消文本并成为''%John%''
构建器自动处理'
选择参数。
编辑
也将您的查询更改为:
String sql = "SELECT * FROM Salary WHERE emp_id in (select _id from Employee where employee_name like ?)";
Cursor c = getDB(mContext).rawQuery(sql, whereArgs);
编辑 2
我用下面的类重新创建了你的设置,我的所有代码都运行得很好。我从用户 John 那里得到了两个结果。我相信问题出在您的数据库创建中,或者您的数据库中没有数据。使用 DDMS 拉取数据库并使用 SQLite 浏览器打开它。检查您的数据库中是否有任何数据。如果是,那么您的表创建类型与 select 语句不匹配。当我调用 GetMyValues() 时,我从游标返回 2 条记录。
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;
}
}
开发参考:
您可以在选择中包含 ?s,它将被 selectionArgs 中的值替换,以便它们出现在选择中。这些值将绑定为字符串