Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get a single row from table

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.

like image 503
Vaishali Sharma Avatar asked Sep 18 '12 08:09

Vaishali Sharma


People also ask

How do I get one row from a table in SQL?

While the table name is selected type CTRL + 3 and you will notice that the query will run and will return a single row as a resultset. Now developer just has to select the table name and click on CTRL + 3 or your preferred shortcut key and you will be able to see a single row from your table.

How can you select a single row?

A single row subquery returns zero or one row to the outer SQL statement. You can place a subquery in a WHERE clause, a HAVING clause, or a FROM clause of a SELECT statement. Contents: Single Row SubQueries in WHERE clause.

How do you get rows from a table?

Click the left border of the table row. The following selection arrow appears to indicate that clicking selects the row. You can click the first cell in the table row, and then press CTRL+SHIFT+RIGHT ARROW.


2 Answers

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();
    }
}
like image 105
vinod Avatar answered Oct 19 '22 13:10

vinod


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());
like image 26
luftinur Avatar answered Oct 19 '22 11:10

luftinur