15

These are the ids in my table:

KEY_ID(autoincremented integer primary key)   KEY_NAME(text)     KEY_PH_NO(text)

1                                             james                   1234567890
2                                             kristein                6484996755
3                                             Roen                    4668798989
4                                             Ashlie                  6897980909

What I want to know is, how can I get a single record from this table on the basis of unique(KEY_ID), for this i have built a getContact() method like this,

Contact getContact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
            KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2));
    // return contact
    return contact;
}

And Contact is a class where I have set all the getter and setter method for all attributes.

Please help with complete code.

ronalchn
  • 12,225
  • 10
  • 51
  • 61
Vaishali Sharma
  • 602
  • 3
  • 6
  • 24

3 Answers3

42

See if this can help you out..

Assuming your table name is Employee

public String getEmployeeName(String empNo) {
    Cursor cursor = null;
    String empName = "";
    try {
        cursor = SQLiteDatabaseInstance_.rawQuery("SELECT EmployeeName FROM Employee WHERE EmpNo=?", new String[] {empNo + ""});
        if(cursor.getCount() > 0) {
            cursor.moveToFirst();
            empName = cursor.getString(cursor.getColumnIndex("EmployeeName"));
        }
        return empName;
    }finally {
        cursor.close();
    }
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
vinod
  • 558
  • 5
  • 10
11
Contact getContact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
        KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
        new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
                        cursor.getString(1), cursor.getString(2));
    // return contact
    return contact;
}

Assuming you have Entity Class for contact, And then you just call it your method above with this.

Contact singleContact = database.getContact(id);

then call contact entity, Im assuming you have method getter getPhone();

Log.d("Phone Number :" , singleContact.getPhone());
Lzh
  • 3,585
  • 1
  • 22
  • 36
luftinur
  • 111
  • 1
  • 4
  • +1 for making your own entities. It's an easy way to keep organized without the overhead of a full blown ORM framework – jmaculate Oct 06 '14 at 14:44
  • @user1085828 inside db.query the first parameter is table name,2nd are the values we want, 3rd is the Where clause ,4th is the value provided, what do the null values after this stand for? – anup Aug 25 '16 at 06:10
-1

Try this

cursor.moveToPosition(position); cursor.getString(listaTarefas1.getColumnIndex("Item"))