選擇ARGS將自動用作字符串。在那裏
String[] whereArgs = new String[] {"%" + mFilter + "%" }; (removed ' from your strings)
有了額外'
它取消了文本,併成爲''%John%''
建設者自動處理'
的選擇ARGS:更改此:
String[] whereArgs = new String[] {"'%" + mFilter + "%'" };
了這一點。
編輯
更改您的查詢也是這樣:
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
我重新創建您的設置與下面的類和我的代碼跑就好了。我從用戶約翰拉出並得到了兩個結果。我相信問題出在你的數據庫創建中,或者你的數據庫中沒有數據。使用DDMS來提取數據庫並用SQLite Browser打開它。檢查數據庫中是否有任何數據。如果確實如此,那麼你的表創建類型不匹配選擇語句。當我調用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;
}
}
Dev Reference:
您可能包括哪些內容?S IN的選擇,這將是由值 代替從selectionArgs兩個,以使他們出現在選擇。該 值將被綁定爲字符串
我試圖刪除'從代碼..但它沒有奏效。 – mudit
@mudit你可以嘗試什麼是我的編輯部分,看看它是否工作 – ObieMD5
我試着用2種方式..它是在firefox的SQLite客戶端上工作,但它不工作在Android上。 – mudit