Good day.
It’s impossible to select the maximum column from the database, I’m doing this:

Cursor cursor = sqliteDB.query(Groups.TABLE_NAME, new String[]{"MAX("+Groups.GroupsColumns.MAX+")"}, null, null, null, null, null); 

tried and so

 Cursor cursor = sqliteDB.rawQuery("SELECT MAX( "+Groups.GroupsColumns.MAX+" ) FROM "+ Groups.TABLE_NAME, null); 

there is data in the cursor

 if (cursor.moveToFirst()) { max = cursor.getInt(cursor.getColumnIndexOrThrow(Groups.GroupsColumns.MAX)); Log.d("log", "max " + max); } 

but when I try to get them I get

  column 'max' does not exist 

the base exists, such a column in it precisely exists, data is normally written to it, read normally, tried for the sake of interest with other columns - the same picture.
Thank you in advance.


it turns out that the entry with the column name 'MAX (max)' is written to the cursor until the solution is found

  • try the alias add. And Groups.GroupsColumns.MAX what does it matter? - Yura Ivanov
  • @Yura Ivanov Groups.GroupsColumns.MAX equals 'max' I work through the contract everywhere, the error with the wrongly specified column name is excluded, it turns out that a record is written to the cursor with the column name 'MAX (max)' has not yet found the solution - gadfil
  • well, and why did you decide that the column name as a result should be Groups.GroupsColumns.MAX right here cursor.getColumnIndexOrThrow(Groups.GroupsColumns.MAX) the column name will be MAX (max). - Yura Ivanov
  • @Yura Ivanov if you use Cursor cursor = sqliteDB.query (Groups.TABLE_NAME, new String [] {"MAX (" + Groups.Groups.Columns.MAX + ")"}, null, null, null, null, null); then yes the column name in the cursor should be 'MAX (max)', but why it is set in a raw query, where it is not explicitly indicated anywhere I don’t know with sqlite I have been working for a long time - gadfil
  • Thanks, it helped. Please make a comment with the answer. - gadfil

1 answer 1

add alias to raw request.

 sqliteDB.rawQuery("SELECT MAX( "+Groups.GroupsColumns.MAX+" ) as "+Groups.GroupsColumns.MAX+" FROM "+ Groups.TABLE_NAME, null); 

everything will work. Without an explicit alias name, no one will rename the column name for you. in mysql, in sqlite ... it will be exactly as written in the select statement. mssql for you would change the column name to column_0.