开发者

android SELECT MAX not work

开发者 https://www.devze.com 2023-03-10 09:21 出处:网络
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?

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 = databaseHelpe开发者_StackOverflowr.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); 
}


Just put

cursordati.moveToFirst();

before

final int  Ingressi = cursordati.getInt(0);

It works :)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号