Lilu Patel
Lilu Patel

Reputation: 321

SQLite, getting last item in table

I need some help with using the database that I created. I need to access the last item in the database. This is my code that gets a single item. How can I change it to only get the last?

// Getting single user
User getuser(int id) {
    SQLiteDatabase db = this.getReadableDatabase();


    Cursor cursor = db.query(TABLE_USER, new String[] { KEY_ID,
            KEY_NAME, KEY_AGE, KEY_WEIGHT, KEY_HEIGHT, 
            KEY_GOAL, KEY_BMI }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);

    if(cursor!=null && cursor.getCount()!=0){
          cursor.moveToFirst();
    }

    User user = new User(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getInt(2),cursor.getInt(3), cursor.getInt(4), 
            cursor.getString(5), cursor.getFloat(6));
    return user;
}

Upvotes: 1

Views: 2344

Answers (5)

prvn
prvn

Reputation: 916

if the id uniquely identifies the user then you will be getting only one row as the result for the id you are passing so you dont have to worry about it is the last or first in the database. if it is just that you want the last row in your database then you dont have to pass an id you can do a select * and after retrieving move the cursor to last ie cursor.moveToLast().

Do this if you just want the last row

User getuser() {
  SQLiteDatabase db = this.getReadableDatabase();


  Cursor cursor = db.query(TABLE_USER, new String[] { KEY_ID,
        KEY_NAME, KEY_AGE, KEY_WEIGHT, KEY_HEIGHT, 
        KEY_GOAL, KEY_BMI }, null,
        null, null, null, null, null);

  if(cursor!=null && cursor.getCount()!=0){
      cursor.moveToLast();
  }

  User user = new User(Integer.parseInt(cursor.getString(0)),
        cursor.getString(1), cursor.getInt(2),cursor.getInt(3), cursor.getInt(4), 
        cursor.getString(5), cursor.getFloat(6));
  return user;
}

and your call can be like this

User currentUser = db.getUser();

If you are passing an id to get a user details an the id uniquely identifies the user then what you have done will work

Upvotes: 0

test test
test test

Reputation: 284

Try this one:

Cursor c = db.rawquery("Select *from tablename");
c.movetoLast();

and if you want to get the data from last to first use this codes:

Cursor c = db.rawquery("Select *from tablename");
c.movetoLast();
int count = c.getCount();
for(int i = 0; i < count; i++){
c.movetoPrevious();
}

Upvotes: 0

Lucifer
Lucifer

Reputation: 29632

You simply need to change a little in your code,

// Getting single user
User getuser(int id) {
    SQLiteDatabase db = this.getReadableDatabase();


    Cursor cursor = db.query(TABLE_USER, new String[] { KEY_ID,
            KEY_NAME, KEY_AGE, KEY_WEIGHT, KEY_HEIGHT, 
            KEY_GOAL, KEY_BMI }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);

    if(cursor!=null && cursor.getCount()!=0){
          cursor.moveToLast();                     // Change here
    }

    User user = new User(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getInt(2),cursor.getInt(3), cursor.getInt(4), 
            cursor.getString(5), cursor.getFloat(6));
    return user;
}

Upvotes: 0

Krauxe
Krauxe

Reputation: 6058

Are you perhaps looking for cursor.moveToLast()? This will move the cursor to the last record selected from your query. You can then get the fields values as usual using cursor.getString(n) etc.

Upvotes: 0

alex
alex

Reputation: 6409

Cursor cursor = db.query(TABLE_USER, new String[] { KEY_ID,
        KEY_NAME, KEY_AGE, KEY_WEIGHT, KEY_HEIGHT, 
        KEY_GOAL, KEY_BMI }, KEY_ID + "=?",
        new String[] { String.valueOf(id) }, null, null, KEY_ID + " DESC", "LIMIT 1");

Upvotes: 1

Related Questions