LuciM
LuciM

Reputation: 1

SQLite "Order by" in query gives wrong order

I have a table with 3 columns: name, date and time being String, String and Double respectively. I want to order the table according to the values of the last columns, but if i have in my database the times of: 1230 800 900 The order I get is 900 800 1230

This is the method I use to order it:

public List<Register> orderAllRegisters() {
    List<Register> registers = new LinkedList<Register>();

    // 1. build the query
    String query = "SELECT  * FROM " + TABLE_REGISTERS + " ORDER BY " + RKEY_TIME + " DESC";

    // 2. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
    String sortOrder =
            RKEY_TIME;

    Cursor cursor = db.rawQuery(query,null);

    // 3. go over each row, build register and add it to list
    Register register = null;
    if (cursor.moveToFirst()) {
        do {
            register = new Register();
            register.setId(Integer.parseInt(cursor.getString(0)));
            register.setName(cursor.getString(1));
            register.setDate(cursor.getString(2));
            register.setTime(cursor.getDouble(3));

            // Add register to registers
            registers.add(register);
        } while (cursor.moveToNext());
    }

    Log.d("resultado", registers.toString());

    // return registers
    return registers;
}

This is how the table is created:

String CREATE_REGISTER_TABLE = "CREATE TABLE registers ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "name TEXT, "+
            "date TEXT, "+
            "time REAL)";

db.execSQL(CREATE_REGISTER_TABLE);

Oh and: private static final String TABLE_REGISTERS = "registers";

To add something to the database i use:

public void addRegister(Register register){
    Log.d("addRegister", register.toString());
    // 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    // 2. create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put(RKEY_NAME, register.getName()); // get name
    values.put(RKEY_DATE, register.getDate());
    values.put(RKEY_TIME, register.getTime());
    // 3. insert
    db.insert(TABLE_REGISTERS, // table
            null, //nullColumnHack
            values); // key/value -> keys = column names/ values = column values

    // 4. close
    db.close();
}

I have tried changing the column "time" to int and strings to see if that was the problem but I also get wrong orders. I also tried putting DOUBLE and INT instead of REAL on the CREATE_TABLE_REGISTER definition but that didn't work either.

If there is any other info that would be useful to solve this case i'd be happy to give. I would be grateful for any help!

Edit: Here's the Register Class:

public class Register {
private int id;
private String name;
private String date;
private double time;

public Register(){}

public Register(String name, String date, double time) {
    super();
    this.name = name;
    this.date = date;
    this.time = time;
}

@Override
public String toString() {
    return "Register [id=" + id + ", name=" + name + ", date=" + date +", time="+ time + "]";
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public double getTime() {
    return time;
}

public void setTime(double time) {
    this.time = time;
}

}

Upvotes: 0

Views: 831

Answers (1)

LuciM
LuciM

Reputation: 1

So the solution was really silly. The code was right but because i had first generated the database putting the "time" value in a TEXT column it didn't change the values that were already there. All I had to do was uninstall the app and generate another database with the new code. Thanks for the insight in the comments!

Upvotes: 0

Related Questions