PDA

View Full Version : QSqlQuery bind issue



ToddAtWSU
13th February 2011, 22:17
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:


QSqlQuery customer;
customer.prepare( "SELECT Fname,Lname,StreetAddress,City,State,ZipCode,Phone FROM customer WHERE Cust_ID = :id " );
customer.bindValue( ":id", customerID );
customer.exec( );
qDebug( ) << customer.lastQuery();

I have also tried


QSqlQuery customer;
customer.prepare( "SELECT Fname,Lname,StreetAddress,City,State,ZipCode,Phone FROM customer WHERE Cust_ID=?" );
customer.addBindValue( customerID );
customer.exec( );
qDebug( ) << customer.lastQuery();

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:

QSqlQuery customer;
customer.prepare( QString( "SELECT Fname,Lname,StreetAddress,City,State,ZipCode,Phone FROM customer WHERE Cust_ID=?" ) );
customer.addBindValue( customerID, QSql::Out );
customer.exec( );
Thanks for the clarification.

wysota
14th February 2011, 00:25
Use QSqlQuery::bindValue() instead of addBindValue().

ChrisW67
14th February 2011, 00:25
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()?