SQlite WHERE语句不起作用?

时间:2013-04-08 21:11:58

标签: java android sql

我正在制作一个应用程序,用于存储书籍标题,添加日期以及该书需要返回的日期。下面,我会显示where语句,该语句可以获取将在今天和之后两天返回的所有书籍,并以列表视图的形式返回。但是,它不起作用,我不知道我哪里出错:

    public Cursor fetchAllDue() {

    //get todays date
    Calendar cal=Calendar.getInstance();
    int currentDay=cal.get(Calendar.DAY_OF_MONTH);

    //Add days on to todays date
    cal.set(Calendar.DAY_OF_MONTH, currentDay+2);
    cal.getTime(); //Result

    //format back to string
    String formattedDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(cal.getTime());


  //get todays date
    Calendar cal1=Calendar.getInstance();
    int currentDay1=cal1.get(Calendar.DAY_OF_MONTH);

    //Atodays date
    cal1.set(Calendar.DAY_OF_MONTH, currentDay1);
    cal1.getTime(); //Result

    //format back to string
    String formattedDate1 = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(cal1.getTime());


    //body = date added, needs to be changed
    return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,
            KEY_BODY, KEY_EXTRA, KEY_DUE }, KEY_DUE + " BETWEEN 'formattedDate1' and 'formattedDate2'", null, null, null, null);
}

这是为"截止日期"输入日期时的代码:

                //convert string to int.
            int myNum = Integer.parseInt(editbox3.getText().toString());

            //get todays date
            Calendar cal=Calendar.getInstance();
            int currentDay=cal.get(Calendar.DAY_OF_MONTH);

            //Add days on to todays date
            cal.set(Calendar.DAY_OF_MONTH, currentDay+myNum);
            cal.getTime(); //Result

            //formate back to string
            String formattedDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(cal.getTime());


            bundle.putString(NotesDbAdapter.KEY_DUE,formattedDate
                    .toString());

日期都是字符串。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

您查询错误。您使用的是formattedDate1formattedDate2,但您的代码设置为formattedDateformattedDate1,更不用说这些因为您没有通过它而导致查询失败的重要事实。它应该是这样的:

String where = KEY_DUE + " BETWEEN ? AND ?";
String[] args = { String.valueOf(formattedDate1), String.valueOf(formattedDate2) };
return mDb.query(DATABASE_TABLE, where, args, null, null, null, null);

答案 1 :(得分:1)

我认为 WebnetMobile 的答案是正确的!

使用Android上的SQLite,我总是使用包含日期的UNIX时间戳的INTEGER字段。因此,我可以更快地执行查询,并且没有WHERE或字符串日期转换问题。

Date date;
...
long timeStamp = date.getTime();

您还可以尝试使用日期作为查询的参数:

Date from, to;
long timeStampFrom = from.getTime();
long timeStampTo = to.getTime();

String[] params = new String[] { String.valueOf(from), String.valueOf(to) };
c = db.rawQuery("SELECT * FROM " + getTableName() + " WHERE DateTime BETWEEN ? AND ?", params);

或者如果您想使用UNIX时间戳:

String[] params = new String[] { String.valueOf(timeStampFrom ), String.valueOf(timeStampTo )) };
c = db.rawQuery("SELECT * FROM " + getTableName() + " WHERE DateTime BETWEEN ? AND ?", params);

答案 2 :(得分:0)

非常肯定这会使伙伴工作。

基本上是另外两个答案的混合。

     String[] params = new String[] { String.valueOf(formattedDate1), String.valueOf(formattedDate2) };

     return mDb.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE duedate BETWEEN ? AND ?", params);

如果有帮助,请告诉我。