M.E.
M.E.

Reputation: 536

Get minimum from SQLite database column?

How can I get the minimum value of an database column? I need to find the minimum _id of a SQLite table.

I tried the following, but had no success:

Cursor c = db.query(MY_DATABASE_TABLE_LAST_REQUEST, new String[] { "min(" + KEY_ROWID + ")" }, null, null,
                    null, null, null);
int rowID = c.getInt(0);

What am I doing wrong?

Upvotes: 9

Views: 5970

Answers (1)

Corey Sunwold
Corey Sunwold

Reputation: 10254

Make sure you call moveToFirst before getting the value:

Cursor c = db.query(MY_DATABASE_TABLE_LAST_REQUEST, new String[] { "min(" + KEY_ROWID + ")" }, null, null,
                null, null, null);  
c.moveToFirst();  //ADD THIS!
int rowID = c.getInt(0);

Upvotes: 12

Related Questions