PDA

View Full Version : QSqlQuery / bindValue syntax



dsant
24th April 2013, 12:50
Hello, this code is working OK :


QSqlQuery query(mybase);
query.prepare("SELECT name FROM mytable");

if( ! query.exec() )
cout << "error SQL= " << query.lastError().text().toStdString() << endl;
else
cout << "Ok" << endl;


But if I change the 3 lines :


QSqlQuery query(mybase);
query.prepare("SELECT :tag_array FROM :tag_table");
query.bindValue(":tag_array","name");
query.bindValue(":tag_table","mytable");

if( ! query.exec() )
cout << "error SQL= " << query.lastError().text().toStdString() << endl;
else
cout << "Ok" << endl;


I have this error at execution time :


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':tag_array FROM :tag_table' at line 1 QMYSQL: Unable to execute query


Did I did a mistake ?

Lesiok
24th April 2013, 12:59
Check the documentation for MySQL if the table name in a prepared statement can be a parameter.

dsant
27th April 2013, 15:29
You're totaly right Lesiok ! it is not a Qt problem, it is a Mysql problem.
Here is 2 good docs :
http://rpbouman.blogspot.fr/2005/11/mysql-5-prepared-statement-syntax-and.html
http://dev.mysql.com/doc/refman/5.0/fr/sqlps.html

You have lightened my light, thanks a lot !