psv
psv

Reputation: 3287

Android query for sqlite with date

I don't understand why my query does not work in Java (android). I would like access to a sqlite databse but I think the problem is about the "WHERE" clause.

Cursor cQuery = db.query("PLUGBATTERY", new String[] {
                        "strftime('%s',date)", "code", "state","startup" }, "strftime('%Y-%m-%d',date)=="+DATE_FORMAT_DATE.format(currentC.getTime()), null, null,null, "strftime('%s',date)");

in the debug view I have DATE_FORMAT_DATE.format(currentC.getTime()) = 2013-08-22

Maybe I can test my query out of my application code but I don't know how to do that...

Thanks for your help.

Upvotes: 1

Views: 1679

Answers (1)

CL.
CL.

Reputation: 180010

In SQL, strings must be written 'with quotes'.

You can avoid formatting problems like this (and SQL injection attacks) by using parameters:

cursor = db.query("PLUGBATTERY",
                  new String[] { "strftime('%s',date)", "code", "state","startup" },
                  "date(date) = ?",
                  new String[] { DATE_FORMAT_DATE.format(currentC.getTime()) },
                  null, null, "date");

Upvotes: 3

Related Questions