문제

I cant get any result if i put "date('now', 'localtime')" in the query.

My original code is:

Cursor record = db.query(RECORD_TABLE, null, date(UTC_DATETIME,'localtime') = ? ",
            new String[]{"date('now', 'localtime')"},null,null,null);

It doesn't return any result.

But I can get result if I use:

Cursor record = db.query(RECORD_TABLE,null, date(UTC_DATETIME,'localtime') = ? ",
            new String[]{"2014-05-13"},null,null,null);

Is there a way that I can use "date('now', 'localtime')" in my query?

도움이 되었습니까?

해결책

The whole point of parameters is to prevent their values from being interpreted as SQL expressions.

To call an SQL function, you have to put it into the SQL string:

Cursor record = db.query(RECORD_TABLE, null,
        "date(UTC_DATETIME,'localtime') = date('now', 'localtime')",
        null,null,null,null);

Please note that the localtime conversion affects both values in the same way, so you can omit it:

Cursor record = db.query(RECORD_TABLE, null,
        "date(UTC_DATETIME) = date('now')",
        null,null,null,null);

or even omit the first date call if the UTC_DATETIME column alread is in the correct yyyy-MM-dd format:

Cursor record = db.query(RECORD_TABLE, null,
        "UTC_DATETIME = date('now')",
        null,null,null,null);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top