• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps sqlite query - Max function

Hello,

I've a problem with a query in sqlite3.

The query is very simple:

SELECT MAX (INIT_DATE) INIT_DATE FROM TABLE;

When my application execute this query, and the table is empty, a record is returned. I can't understand why a record is being returned if the table is empty. It should return cero results.

I've tried to execute the query in two different ways:

cursor = db.query(true, "TABLE", new String[] {"MAX(INIT_DATE) AS INIT_DATE"}, null, null, null, null, null, null);

cursor = db.rawQuery("SELECT MAX(INIT_DATE) AS INIT_DATE FROM TABLE", null);

Could anybody help me?

Thanks in advance!
 
I've moved this to the applications development forum as he is where other devs hang out and will be better suited to answer you questions.:)
 
I agree with mills2533. Max is one of the aggregate functions. They always return one record even if the table is empty. For example, count(*) will return 0 if the table is empty.

For a complete list, please refer to SQLite Query Language: Aggregate Functions. As it says, Aggregate max() returns NULL if and only if there are no non-NULL values in the group.

Hope this helps.
 
Back
Top Bottom