我有两张桌子:
1) Employee -- _id, employee_name.
2) Salary -- _id, amount, emp_id.
示例数据:
Employee:
1 John
2 Rocky
3 Marry
Salary:
1 500 1 //salary for John
2 400 1 //salary for John
3 600 2 //salary for Rocky
4 700 2 //salary for Rocky
5 350 3 //salary for Marry
现在,我想在工资表中搜索,以查看我已经支付了工资的人。如果我在工资表中搜索“John”,它应该返回第1行和第2行,这是John的。
以下是我的尝试:
String where = " emp_id in (select _id from Employee where employee_name like ? )";
String[] whereArgs = new String[] {"'%" + mFilter + "%'" };
Cursor c = getDB(mContext).query("Salary", null, where, whereArgs,
null, null, null);
但总是没有结果。请帮忙。
更新
我调试了代码,发现以下查询正在游标中执行:
SELECT * FROM Salary WHERE emp_id in (select _id from Employee where employee_name like ? );
答案 0 :(得分:2)
选择args自动用作字符串。改变这个:
String[] whereArgs = new String[] {"'%" + mFilter + "%'" };
To This:
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 Browser打开它。检查您的数据库中是否包含任何数据。如果是,则表创建类型与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;
}
}
您可以在选择中包含?,它将被值替换 来自selectionArgs,以便它们出现在选择中。该 值将绑定为字符串
答案 1 :(得分:-2)
我想,使用光标更好,
演示代码:
Cursor c = db.query(TABLE_CONTACTS, columns, KEY_MOBILE + " like '%"
+ mobno + "'", null, null, null, order);
return c;
如果该值的实例在数据库!!
,它将返回该值