SQLite查询中的“排序依据”给出了错误的顺序

时间:2018-10-07 01:37:05

标签: android sqlite sorting sql-order-by

我有一个包含3列的表:名称,日期和时间分别是String,String和Double。我想根据最后一列的值对表进行排序,但是如果我的数据库中有以下时间: 1230 800 900 我得到的命令是 900 800 1230

这是我订购的方法:

<!DOCTYPE html>
<meta charset="utf-8">

<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.9/d3.js"></script>
<div id="line"></div>

<script>

var margin = {top: 10, right: 80, bottom: 20, left: 80},
    width = 560 - margin.left - margin.right,
    height = 100 - margin.top - margin.bottom;


var svg = d3.select("#line").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)

    linechart = svg.append("g")
                      .attr('id', 'removableLineChart')
                      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

      var years = [{year: 2008}, 
                   {year: 2009}, 
                   {year: 2010}, 
                   {year: 2011}, 
                   {year: 2012}];


      var x = d3.time.scale().domain(d3.extent(years, function(d){return d.year})).range([0, width]);
      var xAxis = d3.svg.axis().scale(x);

    linechart.append("g")
        .call(xAxis);
</script>
</body>

这是创建表的方式:

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

db.execSQL(CREATE_REGISTER_TABLE);

哦,还有: 私有静态最终字符串TABLE_REGISTERS =“ registers”;

要使用某些方法向数据库添加内容,

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

我尝试将“时间”列更改为int和字符串,以查看是否是问题所在,但我也得到了错误的命令。 我还尝试将DOUBLE和INT而不是REAL放在CREATE_TABLE_REGISTER定义上,但这也不起作用。

如果有其他信息对解决这种情况非常有用,我很乐意提供。 我将不胜感激!

编辑: 这是寄存器类:

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

}

1 个答案:

答案 0 :(得分:0)

所以解决方案真的很愚蠢。该代码是正确的,但是因为我首先生成了将“时间”值放在TEXT列中的数据库,所以它没有更改已经存在的值。我要做的就是卸载应用程序并使用新代码生成另一个数据库。感谢您的评论见解!