Mone Enom
Mone Enom

Reputation: 29

android SELECT MAX not work

I have tested this select in SQLite and it works but, when I use this in Android, it returns 0.

Where am I going wrong?

Thanks!!

final String idauto = bundle.getString("idtabella");

DatabaseHelper databaseHelper = new DatabaseHelper(this);

final SQLiteDatabase db = databaseHelper.getReadableDatabase();
final Cursor cursordati = db.rawQuery("SELECT MAX('in_auto') AS in_auto "
    + "FROM inout WHERE id_auto ='" + idauto + "'", null);

final int  Ingressi = cursordati.getColumnIndex("in_auto");  
if(cursordati.moveToFirst()){  
    TextV.append("iprova: "+ Ingressi); 
    do {
        TextV.append("dprova: "+ Ingressi); 
    } while (cursordati.moveToNext());
    TextV.append("wprova: "+ Ingressi); 
}

Upvotes: 1

Views: 1171

Answers (2)

Nafti
Nafti

Reputation: 41

Just put

cursordati.moveToFirst();

before

final int  Ingressi = cursordati.getInt(0);

It works :)

Upvotes: 4

paxdiablo
paxdiablo

Reputation: 881113

I'm not sure why you thing you have a problem. You're asking for the column index for a query with exactly one column. Since the index is zero-based, you will get zero.

If you want to get the actual value of max(in_auto) instead of its index, you should probably try:

final int  Ingressi = cursordati.getInt(cursordati.getColumnIndex("in_auto"));

or probably just:

final int  Ingressi = cursordati.getInt(0);

would be fine, as long as you remember to check and possibly change it if you ever modify the query.

Upvotes: 0

Related Questions