Thish代码对我来说很好,它会逐一显示每个员工的信息。 但我只想查看给定身份证的一名员工的信息。 我怎么能这样做。
c=myDbHelper.query("EMP_TABLE", null, null, null, null,null, null);
if(c.moveToFirst())
{
do {
Toast.makeText(CopyDbActivity.this,
"_id: " + c.getString(0) + "\n" +
"E_NAME: " + c.getString(1) + "\n" +
"E_AGE: " + c.getString(2) + "\n" +
"E_DEPT: " + c.getString(3),
Toast.LENGTH_LONG).show();
} while (c.moveToNext());
}
答案 0 :(得分:1)
在Sqlite帮助器类中有一个getRecord方法
public Cursor getRecord(long rowId) throws SQLException
{
Cursor cursor = db.query(EMP_TABLE, new String[] { _id,
E_NAME, E_AGE,E_DEPT }, _id + "=?",
new String[] { String.valueOf(rowId) }, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return mCursor;
}
然后
db.open();
Cursor c = db.getRecord(1); // pass the record id
Toast.makeText(CopyDbActivity.this,
"_id: " + c.getString(0) + "\n" +
"E_NAME: " + c.getString(1) + "\n" +
"E_AGE: " + c.getString(2) + "\n" +
"E_DEPT: " + c.getString(3),
Toast.LENGTH_LONG).show();
答案 1 :(得分:0)
SQLiteDatabase query()
方法的第三个和第四个参数可用于形成SQL WHERE
子句。
例如,要获取_id
为2的所有行:
String[] selectionArgs = {"2"};
c=myDbHelper.query("EMP_TABLE", null, "_id=?", selectionArgs, null, null, null);