Android 使用 ORDER BY 时出现数据类型不匹配错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/818677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
data type mismatch error when using ORDER BY
提问by Bee
I've got an android app using a local sqlite database.
我有一个使用本地 sqlite 数据库的 android 应用程序。
private SQLiteDatabase mDb;
when I run this query I get my Cursor over rows with pid equal to id, as desired:
当我运行此查询时,我根据需要在 pid 等于 id 的行上获取我的光标:
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, null, null);
when I run the following query, aiming to get that same result set, ordered by pid I get "android.database.sqlite.SQLiteException: datatype mismatch"
当我运行以下查询,旨在获得相同的结果集,按 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");
Any ideas?
有任何想法吗?
回答by outis
It looks like you got just a little mixed up. According to the SQLiteDatabase.querydocumentation, the last argument is the LIMITclause. The second to last is the ORDER BYclause.
看起来你只是有点混淆。根据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)
EDIT
编辑
But, there is also another SQLiteDatabase.querywhere ORDER BYwould be last
但是,还有另一个SQLiteDatabase.query地方ORDER BY会是最后
Cursor query (String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy)
回答by Sandip Jadhav
This worked for me
这对我有用
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);
回答by toto
KEY_PID + " = " + "'" + id + "'"
回答by Bit32
Since Orderby is the second last parameter in the query; your query would be like this
由于 Orderby 是查询中的倒数第二个参数;您的查询将是这样的
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, KEY_PID+" DESC", null);
回答by Timweb
If I correctly understood your problem, try this.
如果我正确理解了您的问题,请尝试此操作。
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);

