The SQL expression extract(epoch from horae)
produces a number of seconds since 1/1/1970 so so that's what should be passed to the parameter :hora_bus
.
The QDateTime::fromTime_t(enf.horaE)
indicates that enf.horaE
has this value, however instead of passing ot to the query, it's passing a QDateTime object whose text representation is probably going to be a string with year,month,etc... that can't be compared to a number of seconds.
So try this instead:
query.bindValue(":hora_bus",enf.horaE);
Also the code shouldn't ignore the boolean return values of prepare()
and exec()
. You don't want to try looping within results when the execution of the query has failed.
EDIT1:
indeed when passing a QDateTime set to today to a prepared query similar to yours, QSqlQuery::exec()
returns false with an SQL error invalid input syntax for type double precision
.
EDIT2: it appears QVariant
doesn't support being initialized with a long
so an explicit cast to a different supported type is necessary. I've chosen qlonglong
for a safe larger value:
query.bindValue(":hora_bus",(qlonglong)enf.horaE);
Tested, it worked for me.