PDA

View Full Version : QSqlQuery prepare with placeholders



pdoria
24th March 2012, 17:46
Must be seeing things... but why:


sql = "SELECT * FROM garmin_packets WHERE unit_imei=? AND fmi_packet_id=? AND unique_id=?";
queryCheck.prepare(sql);
queryCheck.bindValue(0, this->connAvlImei);
queryCheck.bindValue(1, FMI_A607_DRIVER_ID_UPDATE);
queryCheck.bindValue(2, driver_id.change_id);
queryCheck.exec ( sql );

produces this:

"SELECT * FROM garmin_packets WHERE unit_imei=? AND fmi_packet_id=? AND unique_id=?"
QSqlError(-1, "QPSQL: Unable to create query", "ERROR: operator does not exist: character =?
LINE 1: SELECT * FROM garmin_packets WHERE unit_imei=? AND fmi_packe...

HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
")

??? :confused:

Btw I made sure to check my stupidity and caffeine levels... all checked out inside reasonable range... ;)

wysota
24th March 2012, 17:54
Why don't you use named placeholders?


query.prepare("SELECT * FROM garmin_packets WHERE unit_imei=:imei AND fmi_packet_id=:pid AND unique_id=:uid");
query.bindValue(":imei", connAvlImei);
query.bindValue(":pid", FMI_A607_DRIVER_ID_UPDATE);
query.bindValue(":uid", driver_id.change_id);
query.exec();

By the way, your error comes from the fact that you are passing the bare query again to exec() bypassing bound values. You should call exec() without any parameters to use the prepared statement.

pdoria
24th March 2012, 18:12
Turns out caffeine levels were ok but stupidity was off the charts... :D

Thx wysota! you rock! ;)

Goddard
29th August 2012, 05:33
Just in case anyone else is on this thread like me. Don't forget if you use a variable name like ":placeholder" don't use single quotes. I spent at least 30 minutes on that stupid mistake.