سؤال

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