Results 1 to 3 of 3

Thread: QSqlQuery bind issue

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery bind issue

    I am trying to perform a QSqlQuery with binding a value and am having problems. I have already executed an Insert with many binded values and that worked fine, but when I try to do a Select with binded values, I have no luck. My code snippet looks like:

    Qt Code:
    1. QSqlQuery customer;
    2. customer.prepare( "SELECT Fname,Lname,StreetAddress,City,State,ZipCode,Phone FROM customer WHERE Cust_ID = :id " );
    3. customer.bindValue( ":id", customerID );
    4. customer.exec( );
    5. qDebug( ) << customer.lastQuery();
    To copy to clipboard, switch view to plain text mode 

    I have also tried

    Qt Code:
    1. QSqlQuery customer;
    2. customer.prepare( "SELECT Fname,Lname,StreetAddress,City,State,ZipCode,Phone FROM customer WHERE Cust_ID=?" );
    3. customer.addBindValue( customerID );
    4. customer.exec( );
    5. qDebug( ) << customer.lastQuery();
    To copy to clipboard, switch view to plain text mode 

    Neither of these work. When I print lastQuery on the first snippet of code, it prints out:

    "SELECT Fname,Lname,StreetAddress,City,State,ZipCode,Phone FROM customer WHERE Cust_ID = :id "
    So it obviously is ignoring the bind. Can I use a bind in this way or am I misunderstanding what binds do? Thanks!


    Added after 38 minutes:


    It looks like I needed to add the QSql::Out as the second argument to the addBindValue( ) function call. Is this the correct thinking about why it works when I write the line this way:
    Qt Code:
    1. QSqlQuery customer;
    2. customer.prepare( QString( "SELECT Fname,Lname,StreetAddress,City,State,ZipCode,Phone FROM customer WHERE Cust_ID=?" ) );
    3. customer.addBindValue( customerID, QSql::Out );
    4. customer.exec( );
    To copy to clipboard, switch view to plain text mode 
    Thanks for the clarification.
    Last edited by ToddAtWSU; 13th February 2011 at 21:17.

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

    Default Re: QSqlQuery bind issue

    Use QSqlQuery::bindValue() instead of addBindValue().
    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. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlQuery bind issue

    Your first attempt looks correct. Your "solution" doesn't look correct, your :id variable is going into the query (QSql::In, the default) not out.

    Have you looked at the return value of exec() and QSqlQuery::lastError()?

Similar Threads

  1. QtConcurrent and boost::bind
    By TheShow in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2009, 14:47
  2. [QGLWidget] Cannot bind texture
    By Macok in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2009, 19:53
  3. UdpSocket bind warning--pls have a look
    By swamyonline in forum Qt Programming
    Replies: 0
    Last Post: 28th August 2008, 11:42
  4. Socket bind error
    By rburge in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2007, 00:18
  5. QUdpSocket bind / connectToHost ?
    By mnemonic_fx in forum Newbie
    Replies: 2
    Last Post: 17th July 2007, 03:20

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.