我有一个使用本地sqlite数据库的Android应用程序。

private SQLiteDatabase mDb;

当我运行此查询时,我根据需要将prsor放在pid等于id的行上:

mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID}, 
    KEY_PID+" = "+id, null, null, null, null, null);        

当我运行以下查询时,旨在获得相同的结果集,按pid排序我得到“ android.database.sqlite.SQLiteException:datatype mismatch

mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID}, 
    KEY_PID+" = "+id, null, null, null, null, KEY_PID+" DESC");

有什么想法吗?

有帮助吗?

解决方案

看起来你有点混淆了。根据 SQLiteDatabase.query 文档,最后一个参数是 LIMIT 子句。倒数第二个是 ORDER BY 子句。

Cursor query (boolean distinct, 
            String table, 
            String[] columns, 
            String selection, 
            String[] selectionArgs, 
            String groupBy, 
            String having, 
            String orderBy, // <-- ORDER BY
            String limit)

修改

但是,还有另一个 SQLiteDatabase.query 其中 ORDER BY 将是最后的

Cursor query (String table, 
            String[] columns, 
            String selection, 
            String[] selectionArgs, 
            String groupBy, 
            String having, 
            String orderBy)

其他提示

这对我有用

String filter = MySQLiteHelper.JOB_ID + "=" + Integer.toString(jobID);
String orderBy =  MySQLiteHelper.LOG_TIME + " DESC";
Cursor cursor = database.query(MySQLiteHelper.LOG_TABLE_NAME, logTableColumns,
        filter, null, null, null, orderBy);
KEY_PID + " = " + "'" + id + "'"

由于Orderby是查询中的倒数第二个参数; 你的查询就像这样

mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID}, 
    KEY_PID+" = "+id, null, null, null, KEY_PID+" DESC", null);

如果我正确理解了您的问题,请尝试此操作。

    String[] columns = new String[] {KEY_PID, KEY_TID};
    String where = KEY_PID + " = " + Integer.toString(id);
    String orderBy = KEY_PID + " DESC";

    Cursor cursor = mDb.query(PT_TABLE, columns, where, null, null, null, orderBy);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top