0

I have a question.

Have a table like this:

(table name: times)
+-------+-----------+---------+
| block | startTime | endTime |
+-------+-----------+---------+
|     1 | 08:00     | 10:00   |
+-------+-----------+---------+

when I use rawquery like this:

String ctime = "'09:45'";
cursor = sdb.rawQuery(String.format("select block from times where startTime < time(%s) and endTime> time(%s)", ctime,ctime), null);

I Can get the correct data "1".

But when I use query like this:

cursor = sdb.query("times", new String[]{"block"}, "startTime <= time(?) and EndTime >= time(?)", new String[]{ctime,ctime}, null, null, null);

I can't get the correct data. The Cursor count is 0.

Why??

3
  • Try adding single quotes around %s. Commented Mar 12, 2015 at 10:40
  • why are you storing time as HH:MM and not as a number of seconds/minutes? Commented Mar 12, 2015 at 10:52
  • HH:MM is a valid time string in SQLite - sqlite.org/lang_datefunc.html Commented Mar 12, 2015 at 11:14

1 Answer 1

1

A valid time string must not contain quotes.

The quotes in your ctime variable are correct when you are constructing the SQL statement by hand, but they are used to delimit the SQL string and are not part of the value of the SQL string.

When you are using parameters, you do not need to put quotes around strings (unless you actually want them to be part of the string's value).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.