【问题标题】:SQLite Query Confusion, Less than Greater thanSQLite 查询混淆,小于大于
【发布时间】:2013-08-23 16:54:35
【问题描述】:

我给一个函数一个 DAY 和一个 HOUR,我想在两个 text-not-null 列 COLUMN_FROM1 和 COLUMN_TO1 之间获取 COLUMN_DAY1=day 和 HOUR。奇怪的是,如果我说第 7 个小时,并且 FROM1 和 TO1 分别包含 6 和 9,它将返回一个肯定的搜索。如果我给出第 12 小时并且 FROM1 和 TO1 分别包含 11 和 17,则搜索有效。

但是,当我给出 7 并且 FROM1 和 TO1 分别包含 6 和 10 时,搜索不起作用。我认为这与 10 是两位数和 6 是一位数或沿着这些线有关。 下面是我使用的游标查询,请帮忙,我做错了什么?

Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
                allColumns, MySQLiteHelper.COLUMN_DAY1 +" ='" + Day+
                        "' AND " +MySQLiteHelper.COLUMN_FROM1 + " <=" + Hour+
                        " AND " +MySQLiteHelper.COLUMN_TO1+ " >" +Hour
                        , null, null, null, null);

编辑:当 COLUMN_FROM1 包含 6 并且 COLUMN_TO1 包含 10 时,它也应该返回 true。

将数据写入 SQLite 数据库的函数:

InputStream is =getResources().openRawResource(R.raw.ems_data);
        BufferedInputStream bis = new BufferedInputStream(is);

        ByteArrayBuffer baf = new ByteArrayBuffer(50);

        int current = 0;

        while ((current = bis.read()) != -1) {

            baf.append((byte) current);

        }

        byte[] myData = baf.toByteArray();
        String dataInString = new String(myData);
        String[] lines = dataInString.split("\n");

        for (int i=0; i<lines.length; i++){
            comment = datasource.createComment(lines[i]);
            // adapter.add(comment);
        }

编辑:

createComment();功能:

public Comment createComment(String comment) {
        ContentValues values = new ContentValues();
        //parse data in string comment
        String[] words = comment.split("\\t");

        values.put(MySQLiteHelper.COLUMN_COMMENT, comment);
        values.put(MySQLiteHelper.COLUMN_NAME, words[0]); //adds to column "name"
        values.put(MySQLiteHelper.COLUMN_CONTACT, words[1]);
        values.put(MySQLiteHelper.COLUMN_DAY1, words[2]);
        values.put(MySQLiteHelper.COLUMN_FROM1, words[3]);
        values.put(MySQLiteHelper.COLUMN_TO1, words[4]);
        values.put(MySQLiteHelper.COLUMN_DAY2, words[5]);
        values.put(MySQLiteHelper.COLUMN_FROM2, words[6]);
        values.put(MySQLiteHelper.COLUMN_TO2, words[7]);

        //expected error above after DAY2 since it can be NULL

        long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
                values);
        Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
                allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
        cursor.moveToFirst();
        Comment newComment = cursorToComment(cursor);
        cursor.close();
        return newComment;
    }

【问题讨论】:

  • 您的描述不清楚。应该返回哪些记录?
  • @cl- 我取得了一些进展,所以我完全编辑了这个问题。请再看一遍。
  • 数据库中的值和变量的类型是什么?请显示插入 from/to 值的代码。
  • 它们是不为空的文本。我将添加代码。我将它从 TXT 复制到数据库。
  • 插入 from/to 值的代码将在 createComment 函数中。

标签: android database search sqlite


【解决方案1】:

找到答案了!

解析未正确完成,行尾字符 \r 导致 SQLite 数据库无法将结尾“9”识别为9 的问题。添加了这一行:

comment = comment.replaceAll("(\\r|\\n)", "");

在我使用 \t 分隔符解析数据之前,它工作了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2014-06-07
    • 1970-01-01
    相关资源
    最近更新 更多