SQLite查询混淆,小于大于

时间:2013-08-23 16:54:36

标签: android database search sqlite

我给了一个功能一天和一小时,我想在两个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;
    }

1 个答案:

答案 0 :(得分:0)

找到了答案!

解析未正确完成并且行尾字符\ r \ n导致问题,其中SQLite数据库未将结束'9'识别为9。添加了这一行:

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

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