Sqlite max找不到列名

时间:2014-01-11 16:39:33

标签: java android sqlite

使用:

    SQLiteDatabase db = this.getWritableDatabase();
    int max_request_code = 0;
    final String MY_QUERY = "SELECT MAX(" + REQUEST_CODE + ") FROM " + TABLE_TASKS;
    Cursor mCursor = db.rawQuery(MY_QUERY, null);  
          if (mCursor.getCount() > 0) 
          {
            mCursor.moveToFirst();
            max_request_code = mCursor.getInt(mCursor.getColumnIndex(REQUEST_CODE));
          }
          else
              return 0;
          return max_request_code;

给了我一个例外:

  

java.lang.IllegalStateException:无法从CursorWindow读取第0行,col -1。

在包含mCursor.getInt。

的行上

这是我创建表格的方式:

String CREATE_TASKS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_TASKS + "("
            +UUID + " TEXT,"+ REQUEST_CODE + " INT,"+ ACTION + " TEXT,"
            + TIME + " TEXT," + DAY + " TEXT," + RECEPIENTS+  " TEXT," +BODY + " TEXT,"  +SUBJECT +
            " TEXT," +STATUS + " TEXT," +REPEAT + " TEXT," +TASK_NAME + " TEXT" +")";
    db.execSQL(CREATE_TASKS_TABLE);

1 个答案:

答案 0 :(得分:0)

使用单词AS为返回的MAX值指定列名:

final String MY_QUERY = "SELECT MAX(" + REQUEST_CODE + ") AS maxRequest_Code FROM " + TABLE_TASKS;  

然后从游标中获取值:

max_request_code = mCursor.getInt(mCursor.getColumnIndex("maxRequest_Code"));  

或使用:

max_request_code = mCursor.getInt(0);