Selecting Specific Rows from Sqlite Database-Android-Sqlite Database

Get specific row of SQLite android

For your first question, you can add below function and use it.

public User getSingleUserInfo(String email){

SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM " + TABLE_USER + " WHERE " + COLUMN_USER_EMAIL + "=" + email);
cursor.moveToFirst();

//setting related user info in User Object
User user = new User();
user.setUserId(cursor.getInt(cursor.getColumnIndexCOLUMN_USER_ID ));
user.setFirstName(cursor.getString(cursor.getColumnIndex(COLUMN_USER_FIRST_NAME));
user.setLastName(cursor.getString(cursor.getColumnIndex(COLUMN_USER_LAST_NAME ));
user.setEmail(cursor.getString(cursor.getColumnIndex(COLUMN_USER_EMAIL ));
user.setPhoneNumber(cursor.getString(cursor.getColumnIndex(COLUMN_USER_PHONE_NUMBER ));
user.setBirthday(cursor.getString(cursor.getColumnIndex(COLUMN_USER_BARTH_DAY ));
user.setPassword(cursor.getString(cursor.getColumnIndex(COLUMN_USER_PASSWORD));
user.setGender(cursor.getString(cursor.getColumnIndex(COLUMN_USER_GENDER ));

//close cursor & database
cursor.close();
database.close();

return user;

}

Note : Becareful about your User object's setter functions which may be vary from my example and data types. Use c.getInt() if it is int and c.getString() if String.

how to select particular row data in SQLite db and pass it to other activity in Android Studio

You have three issues.

  • First you are getting the column index of the column not data from the column itself.
  • Second you are not moving to a valid row from which to retrieve the data.
  • Third, according to your comments you are extracting a java integer (int) when according to your comments the number is too large for an integer (hence why you get 1410065407), so you need to use a long.

The following code should work:-

    public long senderMobile(String activeuser){ //<<<< NOTE long
long rv = -1; //<<<< default return to indicate no such row
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery("select mobile from user where email=?",new String[]{activeuser});
if (cursor.moveToFirst) { //<<<< Move to the first row (should only be 1)
rv = cursor.getLong(cursor.getColumnIndex("mobile"); //<< get the data from the column
}
return rv;
}
  • You should check the value returned from the senderMobile, -1 indicates that no such row was found and code accordingly.
  • Note the above code is in-principle, it has not been tested so may contain errors.

Pull Data From Specific Row in SQLiteDatabase

You have to create method in database to fetch single row. Create method as below in DbHelper class

 public UserData getSingleUserDetial(String userId) {
SQLiteDatabase db = this.getWritableDatabase();
UserData userData = null;

Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_USERdETAIL + " WHERE " + _ID + "= ?", new String[]{userId});

try {
while (cursor.moveToNext())
{
userData = new UserData();
userData.name = cursor.getString(cursor.getColumnIndex(NAME));
userData.quantity = cursor.getString(cursor.getColumnIndex(QUANTITY));
userData.description = cursor.getString(cursor.getColumnIndex(DESCRIPTION));

}
cursor.close();
} catch (Exception e) {
Log.d(TAG, "Error while trying to get data from database");
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}

return userData;
}

And Fetch in ItemEditActivity like

UserData userData = dbHelper.getSingleUserDetial(userId);

Selecting one row from sqlite database using rawQuery and rowid in Android

If I correctly understand you need to return value of every column from the single row. Since you select only single row and want to get value from every column you need only to get the index of every column. and next iterate through them. Here is the code how it can be done.

Cursor cursor = db.rawQuery("SELECT * FROM Dairy WHERE ROWID = 1 Limit 1", null);

if (cursor.moveToFirst()) {
String[] columnNames = cursor.getColumnNames();
yData = new int[columnNames.length];

for (int i = 0; i < columnNames.length; i++) {
// Assume every column is int
yData[i] = cursor.getInt(cursor.getColumnIndex(columnNames[i]));
}
}

cursor.close();
db.close();

How to retrieve a specific row using SQLite and Cursors?

try this

db.query(tableName, null, "username = ?", new String[]{"username"}, null, null, null);


Related Topics



Leave a reply



Submit