Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying the max value of a column in SQLite For Android

Tags:

android

sqlite

I have a database created and filed with data. There is a column where i want to find the biggest value.

This is the method used in my database adapter :

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

It should work but when i call the method :

        cursor = dbadp.getBiggestInTheColumn(); 

I Get a Runtime Error Like This(LogCat) :

07-14 12:38:51.852: ERROR/AndroidRuntime(276): Caused by: android.database.sqlite.SQLiteException: misuse of aggregate function MAX(): , while compiling: SELECT * FROM spendings WHERE MAX(price)

Any ideas? I suspect this is due to the bad query, but this is the best that i can come up with. Other queries are working well.

like image 238
ilcredo Avatar asked Jul 14 '11 12:07

ilcredo


People also ask

How do I find the maximum value of a column in SQL?

To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.

What is use of SQLite Max aggregate function?

The SQLite MAX function is an aggregate function that returns the maximum value of all values in a group. You can use the MAX function to accomplish a lot of things. For example, you can use the MAX function to find the most expensive products, find the biggest item in its group, etc.

Can we use Max on a column?

MAX can be used with numeric, character, and datetime columns, but not with bit columns.

What is the maximum size of SQLite database in Android?

Maximum Database Size 140 tb but it will depends on your device disk size.


1 Answers

If you just want the biggest value then you want the SQL equivalent of: SELECT MAX(price) FROM spendings.

I tend to use db.rawQuery(String sql, String[] selectionArgs), so it would be: db.rawQuery("SELECT MAX(price) FROM spendings", null).

But I think you could do this with db.query(DATABASE_TABLE, new String [] {"MAX(price)"}, null, null, null, null, null);. Though I haven't tested that.

like image 173
stephendnicholas Avatar answered Sep 20 '22 07:09

stephendnicholas