【问题标题】:data type mismatch error when using ORDER BY使用 ORDER BY 时出现数据类型不匹配错误
【发布时间】:2010-10-23 13:14:21
【问题描述】:

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

private SQLiteDatabase mDb;

当我运行这个查询时,我会根据需要在 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");

有什么想法吗?

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    看起来你有点搞混了。根据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)
    

    【讨论】:

    • 在文档中看起来也有一个版本:SQLiteDatabase.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
    • 错了,提到的doc还给了以下电话query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
    • @2ndGAB:实际上,您提到的方法与 Dandre 提到的方法和 Sandip 在他的回答中使用的方法相同,而不是 Bee 的问题。请注意,它们有不同的 arities(7 对 9),而您的不带布尔值(distinct,来自问题中使用的方法)。
    • 这个“KEY_PID+”=“+id”不是容易被SQL注入吗?我的意思是如果你用字符串做这个..
    【解决方案2】:

    这对我有用

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

    【讨论】:

      【解决方案3】:
      KEY_PID + " = " + "'" + id + "'"
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          如果我正确理解你的问题,试试这个。

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

          【讨论】:

          • 添加一些解释,说明此答案如何帮助 OP 解决当前问题
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多