在android中查询Sqlite数据库

时间:2013-06-19 18:00:10

标签: android android-sqlite android-datepicker android-query

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());
        }

2 个答案:

答案 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);