Results 1 to 4 of 4

Thread: QSqlQuery prepare with placeholders

  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSqlQuery prepare with placeholders

    Must be seeing things... but why:

    Qt Code:
    1. sql = "SELECT * FROM garmin_packets WHERE unit_imei=? AND fmi_packet_id=? AND unique_id=?";
    2. queryCheck.prepare(sql);
    3. queryCheck.bindValue(0, this->connAvlImei);
    4. queryCheck.bindValue(1, FMI_A607_DRIVER_ID_UPDATE);
    5. queryCheck.bindValue(2, driver_id.change_id);
    6. queryCheck.exec ( sql );
    To copy to clipboard, switch view to plain text mode 

    produces this:
    Qt Code:
    1. "SELECT * FROM garmin_packets WHERE unit_imei=? AND fmi_packet_id=? AND unique_id=?"
    2. QSqlError(-1, "QPSQL: Unable to create query", "ERROR: operator does not exist: character =?
    3. LINE 1: SELECT * FROM garmin_packets WHERE unit_imei=? AND fmi_packe...
    4.  
    5. HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
    6. ")
    To copy to clipboard, switch view to plain text mode 

    ???

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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery prepare with placeholders

    Why don't you use named placeholders?

    Qt Code:
    1. query.prepare("SELECT * FROM garmin_packets WHERE unit_imei=:imei AND fmi_packet_id=:pid AND unique_id=:uid");
    2. query.bindValue(":imei", connAvlImei);
    3. query.bindValue(":pid", FMI_A607_DRIVER_ID_UPDATE);
    4. query.bindValue(":uid", driver_id.change_id);
    5. query.exec();
    To copy to clipboard, switch view to plain text mode 

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    pdoria (24th March 2012)

  4. #3
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlQuery prepare with placeholders

    Turns out caffeine levels were ok but stupidity was off the charts...

    Thx wysota! you rock!

  5. #4
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlQuery prepare with placeholders

    Just in case anyone else is on this thread like me. Don't forget if you use a variable name like "laceholder" don't use single quotes. I spent at least 30 minutes on that stupid mistake.

Similar Threads

  1. Replies: 1
    Last Post: 8th August 2011, 01:21
  2. Why does QSqlQuery::prepare() return false?
    By NotANoob in forum Qt Programming
    Replies: 2
    Last Post: 20th April 2011, 19:05
  3. QSqlQuery::prepare() - placeholder as table name
    By dawwin in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2011, 21:40
  4. QSqlQuery prepare() bindValue() broken badly
    By RolandHughes in forum Qt Programming
    Replies: 4
    Last Post: 14th November 2008, 18:25
  5. Database and QSqlQuery::prepare problem
    By fengtian.we in forum Qt Programming
    Replies: 1
    Last Post: 31st October 2007, 23:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.