Morton
Morton

Reputation: 5760

How do i get the minimum value succeed?

My sql table like this:

enter image description here

I want to get the maximum and minimum value , i try the method like this:

//get the highest value
public String getMax(String column_name) {// use the data type of the column
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.query(TABLE_CONTACTS, new String[]{"MAX(" +column_name + ") AS MAX"}, null, null, null, null, null);

    cursor.moveToFirst(); // to move the cursor to first record
    int index = cursor.getColumnIndex("MAX");
    String data = cursor.getString(index);// use the data type of the column or use String itself you can parse it

    db.close();

    System.out.println("maxData:"+data);
    return data;
}

//get the minimum value
public String getMin(String column_name) {// use the data type of the column
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.query(TABLE_CONTACTS, new String[]{"MIN(" +column_name + ") AS MIN"}, null, null, null, null, null);

    cursor.moveToFirst(); // to move the cursor to first record
    int index = cursor.getColumnIndex("MIN");
    String data = cursor.getString(index);// use the data type of the column or use String itself you can parse it

    db.close();

    System.out.println("minData:"+data);
    return data;
}

and i call the method and setText on six TextView:

The result show the max value is fine , the problem is minimum value , some kind of minimum is null.

private void setMinMaxValue(){
    minBeMorning.setText(db.getMin("bMorning"));
    minAfMorning.setText(db.getMin("aMorning"));
    minBeNoon.setText(db.getMin("bNoon"));
    minAfNoon.setText(db.getMin("aNoon"));
    minBeNight.setText(db.getMin("bNight"));
    minAfNight.setText(db.getMin("aNight"));

    maxBeMorning.setText(db.getMax("bMorning"));
    maxAfMorning.setText(db.getMax("aMorning"));
    maxBeNoon.setText(db.getMax("bNoon"));
    maxAfNoon.setText(db.getMax("aNoon"));
    maxBeNight.setText(db.getMax("bNight"));
    maxAfNight.setText(db.getMax("aNight"));
}

Here is my log cat:

06-26 09:43:45.115 22517-22517/? I/System.out: minData:71
06-26 09:43:45.116 22517-22517/? I/System.out: minData:112
06-26 09:43:45.118 22517-22517/? I/System.out: minData:
06-26 09:43:45.119 22517-22517/? I/System.out: minData:
06-26 09:43:45.121 22517-22517/? I/System.out: minData:
06-26 09:43:45.122 22517-22517/? I/System.out: minData:
06-26 09:43:45.125 22517-22517/? I/System.out: maxData:98
06-26 09:43:45.126 22517-22517/? I/System.out: maxData:99
06-26 09:43:45.128 22517-22517/? I/System.out: maxData:96
06-26 09:43:45.129 22517-22517/? I/System.out: maxData:142
06-26 09:43:45.131 22517-22517/? I/System.out: maxData:98
06-26 09:43:45.132 22517-22517/? I/System.out: maxData:151

I think the minimum value of bNoon aNoon bNight aNight should be 93 122 95 132 , but they are null now.

Some one can teach me what step i miss it , thanks in advance.

Upvotes: 1

Views: 46

Answers (1)

VinayagaSundar
VinayagaSundar

Reputation: 1701

Maybe you've defined bNoon, aNoon, bNight and aNight as TEXT change that value into INTEGER and try your code.

Upvotes: 1

Related Questions