无法查询数据库中的最大数量

时间:2012-09-19 16:24:46

标签: android database cursor max

我正在尝试在我的数据库中的一个表的列中找到最大数字。

我以为我对这个问题进行了排序(之前我发过了类似的问题),但经过一些测试后我发现我的代码没有按照我的想法运行。

数据库由一个包含以下列的表组成:

_id,inspection_link,area_number,area_reference

我在数据库助手类中创建了以下代码:

public static final String AREAS_TABLE = "areas";
public static final String AREA_ID = "_id";
public static final String AREA_NUMBER = "area_number";
public static final String AREA_REF = "area_reference";
public static final String AREA_LINK = "area_link";
public static final String INSPECTION_LINK = "inspection_link";

public Cursor selectMaxAreaNumber (long inspectionId) {
    String inspectionIdString = String.valueOf(inspectionId);
    String[] tableColumns = new String[] { 
            AREA_NUMBER, 
            "(SELECT max(" + AREA_NUMBER + ") FROM " + AREAS_TABLE + ") AS max" 
        }; 
    String whereClause = INSPECTION_LINK + " = ?";
    String[] whereArgs = new String[] {
            inspectionIdString          
        };
    Cursor c = rmDb.query(AREAS_TABLE, tableColumns, whereClause, whereArgs, 
            null, null, null);


    if (c != null) {
        c.moveToFirst();
    }
    c.close();
    return c;
}

然后在我想查询数据库的活动中,我写了以下内容:

public class AreaEdit extends Activity {

private EditText AreaNumber;
private EditText AreaReference;
private Button saveButton;
private Button cancelButton;
protected boolean changesMade;
private AlertDialog unsavedChangesDialog;


private RMDbAdapter rmDbHelper;
private long inspectionId;
private long areaId;
private int nextAreaNumber = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    rmDbHelper = new RMDbAdapter(this);
    rmDbHelper.open();
    Intent i = getIntent();
    inspectionId = i.getLongExtra("Intent_InspectionID", -1);
    areaId = i.getLongExtra("Intent_AreaID", -1);
    if (areaId == -1) {
        Cursor c = rmDbHelper.selectMaxAreaNumber(inspectionId);
        startManagingCursor(c);
        c.moveToFirst();
        nextAreaNumber = c.getInt(c.getColumnIndex("max")) + 1; 
    }
    setContentView(R.layout.edit_area);
    setUpViews();
    populateFields();
    setTextChangedListeners();
}

private void setUpViews() {
    AreaNumber =(EditText)findViewById(R.id.area_number);
    AreaReference =(EditText)findViewById(R.id.area_reference);
    saveButton = (Button)findViewById(R.id.area_save_button);
    cancelButton = (Button)findViewById(R.id.area_cancel_button);


}

  private void populateFields() {
        if (areaId > 0) {
            Cursor c = rmDbHelper.fetchArea(areaId);
            startManagingCursor(c);
            c.moveToFirst();
            AreaNumber.setText(c.getString(
                    c.getColumnIndexOrThrow(RMDbAdapter.AREA_NUMBER)));
            AreaReference.setText(c.getString(
                    c.getColumnIndexOrThrow(RMDbAdapter.AREA_REF)));
            c.close();
        }
        else {
            AreaNumber.setText(String.valueOf(nextAreaNumber));
        }
    }

然而,当它返回错误的数字时 - 它似乎从整个表中获取最大数字,其中包括来自其他检查的数据。

我想这可能取决于Strings和Longs之间的转换等等,但我有一个砖墙吗?

任何帮助都非常感激。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方式:

String sql = "SELECT MAX(ColumnNameHere) AS MaxValue FROM myTable WHERE AnotherColumn = 'SomValue'";
Cursor c = db.rawQuery(sql, null); 
c.moveToFirst();
c.getInt(c.getColumnIndex("MaxValue"));

答案 1 :(得分:0)

此处找到此问题的详细解决方案:

以下帖子详细解决了这个问题:SELECT statement not returning MAX number

基本上,这是一个问题的查询问题以及我如何使用光标。