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:
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();
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();
To copy to clipboard, switch view to plain text mode
I have also tried
customer.prepare( "SELECT Fname,Lname,StreetAddress,City,State,ZipCode,Phone FROM customer WHERE Cust_ID=?" );
customer.addBindValue( customerID );
customer.exec( );
qDebug( ) << customer.lastQuery();
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();
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:
customer.
prepare( QString( "SELECT Fname,Lname,StreetAddress,City,State,ZipCode,Phone FROM customer WHERE Cust_ID=?" ) );
customer.addBindValue( customerID, QSql::Out );
customer.exec( );
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( );
To copy to clipboard, switch view to plain text mode
Thanks for the clarification.
Bookmarks