Results 1 to 6 of 6

Thread: QSqlQuery::executedQuery () -- Bound values versus place holders

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    28
    Thanked 3 Times in 3 Posts

    Default QSqlQuery::executedQuery () -- Bound values versus place holders

    After doing an insert to a local MySql database, I want to log the SQL text to a file and use it later for inserting to a different ( remote) database when a connection is available.

    The docs for QSqlQuery::executedQuery () say:

    The placeholders in the original query are replaced with their bound values to form a new query.

    But, for an insert, I see "?" for all the values :

    "INSERT INTO TEAM_RECORDS (event_id, race, heat, today, left_team_time, left_start_time, left_result, right_team_time, right_start_time, right_result) VALUES ( ?,?,?,?,?,?,?,?,?,?)"
    I also tried QSqlQuery::lastQuery, but the Sql text has the placeholders, not the values:

    "INSERT INTO TEAM_RECORDS (event_id, race, heat, today, left_team_time, left_start_time, left_result, right_team_time, right_start_time, right_result) VALUES ( :event_id,:race,:heat,:today,:left_team_time,:left _start_time,:left_result,:right_team_time,:right_s tart_time,:right_result)"
    Is this the intended behavior?

    I can write a little code to parse the SQL returned by ::lastQuery and replace the placeholders with values from the bindValues, but thought I'd ask if I might be missing something already in the class.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: QSqlQuery::executedQuery () -- Bound values versus place holders

    This "placeholders in the original query are replaced with their bound values to form a new query." only occurs if "a prepared query with placeholders is executed on a DBMS that does not support it". Otherwise, as the docs say, this generally returns the same as lastQuery() because Qt never builds a query string with the values replaced in that case.

    I can write a little code to parse the SQL returned by ::lastQuery and replace the placeholders with values from the bindValues, but thought I'd ask if I might be missing something already in the class.
    Why would you want to do that? You can certainly make a much better error or log message than to simply dump the equivalent raw SQL (which you will have to be careful to escape correctly).

  3. #3
    Join Date
    Jun 2012
    Posts
    219
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    28
    Thanked 3 Times in 3 Posts

    Default Re: QSqlQuery::executedQuery () -- Bound values versus place holders

    I don't want a human readable message. I wanted the actual Sql command with values substituted so it could be used directly to insert into another database.

    I just wrote the code to build use the SQL for the "prepare" and the key/value pairs from the bind values. Encapsulated in xml and then parse in remote server that uses it.

    It all works--I just thought it would be simpler to have SQL with the values substituted rather than the additional complexity.

    Thanks,

    Dave Thomas

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

    Default Re: QSqlQuery::executedQuery () -- Bound values versus place holders

    I think it would make more sense to use some standard replication scheme.

    E.g.: http://dev.mysql.com/doc/refman/5.0/en/replication.html
    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.


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

    davethomaspilot (21st January 2014)

  6. #5
    Join Date
    Jun 2012
    Posts
    219
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    28
    Thanked 3 Times in 3 Posts

    Default Re: QSqlQuery::executedQuery () -- Bound values versus place holders

    This is definitely the way to go. Use MySql replication rather than "roll your own".

    I needed to read the docs a couple of times to get comfortable with the configuration process. It sounded much more complicated than what it really is.

    Thanks for the good advice!

    Works great on servers that I can admin. Now, I either need to change web hosting services to get one that will allow me to configure a mysql server, or figure out to encapsulate the equivalent of the bin-log files into something I can do an http post with.

    Thanks!

    Dave Thomas
    Last edited by davethomaspilot; 21st January 2014 at 12:13. Reason: spelling corrections

Similar Threads

  1. Replies: 14
    Last Post: 16th May 2017, 03:51
  2. Replies: 4
    Last Post: 22nd August 2013, 16:09
  3. Replies: 1
    Last Post: 8th August 2011, 01:21
  4. Replies: 7
    Last Post: 3rd November 2010, 23:18
  5. Replies: 1
    Last Post: 1st August 2010, 14:06

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.