0

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

2 Answers 2

1

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();  
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.