2011-06-02 37 views
0

我已經測試了這個選擇在SQLite和它的工作原理,但是,當我使用這在Android中,則返回0。的Android SELECT MAX不行

我要去哪裏錯了?

謝謝!

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); 
} 

回答

0

我不知道爲什麼你有什麼問題。對於只有一列的查詢,您要求列索引。由於該指數是基於零的,所以你的得到零。

如果你想獲得實際值的max(in_auto)而不是它的指數,你應該嘗試:

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

或可能只是:

final int Ingressi = cursordati.getInt(0); 

將被罰款,只要因爲你記得檢查並可能改變它,如果你修改查詢。

+0

無法工作給予錯誤之前! 06-02 11:45:16.874:錯誤/ AndroidRuntime(6111):引起:android.database.CursorIndexOutOfBoundsException:請求索引-1,大小爲1 – 2011-06-02 11:47:11

+0

然後,您得到-1列索引,而不是如你似乎已經說過的那樣。你需要先弄清楚。我可以問爲什麼你有max('in_auto')'而不是'max(in_auto)'? – paxdiablo 2011-06-02 11:54:43

+0

測試al MAX(x),MAX('x')和「'」+ idauto +「'」「+ idauto +」「同樣的錯誤... 索引-1請求,大小爲1 – 2011-06-02 12:08:55

3

只要把

cursordati.moveToFirst(); 

final int Ingressi = cursordati.getInt(0); 

它的工作原理:)