rani
rani

Reputation: 613

How to find the maximum value of the column in sqlite database

public Cursor getBiggestInTheColumn() 
{
     return db.query(HEADER_TABLE, null,"MAX(key_Division)", null, null, null, null);
}

and I call this method as Cursor cc=db.getBiggestInTheColumn();

But it forcefully close the application.Please help me

Upvotes: 3

Views: 15449

Answers (2)

muffinmad
muffinmad

Reputation: 641

public int getBiggestInTheColumn() 
{
    Cursor c = db.query(HEADER_TABLE, null,new String[] {"MAX(key_Division)"}, null, null, null, null); 
    try {
        c.moveToFirst();
        return c.getInt(0);
    } finally {
        c.close();
    }
}

Upvotes: 5

Ricky
Ricky

Reputation: 7889

You can use simpleQueryForLong and cast it accordingly.

public int getMaxColumnData() {

    mDb = mDbManager.getReadableDatabase();
    final SQLiteStatement stmt = mDb
            .compileStatement("SELECT MAX(column) FROM Table");

    return (int) stmt.simpleQueryForLong();
}

Upvotes: 25

Related Questions