0

I want to get entries' date for a given month and year, for that I execute successfully in SQliteman the next query:

SELECT date FROM Entries WHERE strftime('%Y-%m',date) = '2013-04'

To use this query with QSqlQuery I use the following code

query.prepare("SELECT date FROM Entries WHERE strftime('%Y-%m',date) = ':year-:month'");
query.bindValue(":month", QString().sprintf("%02d", month));
query.bindValue(":year", QString::number(year));

But the error "Parameter count mismatch" is raised. That is for the quotes in :year and :month, but I have to use it or the query does not return any result.

How must the query be built if the quotes cannot be used?

Manuel
  • 2,236
  • 2
  • 18
  • 28

1 Answers1

0

You cannot replace parameters inside strings; parameters themselves are entire strings.

Concatenate the strings in SQL:

query.prepare("SELECT ... WHERE strftime(...) = :year || '-' || :month");

You could also construct the entire string beforehand:

query.prepare("SELECT ... WHERE strftime(...) = :yyyymm");
query.bindValue(":yyyymm", month + "-" + year);
CL.
  • 173,858
  • 17
  • 217
  • 259