Reputation: 91
Thish code work fine for me it show the information of every employee one by one. BUT i want to see only the information of the one employee of given id. how can i do that .
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());
}
Upvotes: 0
Views: 4660
Reputation: 133560
Have a getRecord method in your Sqlite helper class
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;
}
Then
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();
Upvotes: 1
Reputation: 67189
The third and fourth parameters of the SQLiteDatabase query()
method can be used to form a SQL WHERE
clause.
For example, to get all rows with where _id
is 2:
String[] selectionArgs = {"2"};
c=myDbHelper.query("EMP_TABLE", null, "_id=?", selectionArgs, null, null, null);
Upvotes: 0