QT SQL bindValue issue

44 views Asked by At

I got problem for using the bindValue in QT with SQLite3. The below statement don't work.The exec is success but result incorrect.

 query5.prepare("select DISTINCT :col_readback FROM Equipment");
 query5.bindValue(":col_readback", "Category");

However, if I run the below one statement without any issue.

query5.prepare("select DISTINCT Category FROM Equipment");

I have tried to change to another name of the bindValue but it don't work as well.

2

There are 2 answers

1
guitarpicva On

Edited out the original answer so the first comment makes no sense now.

Given that you are trying to substitute a column name vs. a value, I would suggest that you simply build the query string and then execute it, skipping the prepare.

const QString col = myValFromProgram; // a QString
query5.exec("select DISTINCT " + col + " FROM Equipment");
1
richard lee On

Look like bindValue have some problem to work with column field. I use arg instead and it work query5.prepare(QString("select DISTINCT %1 FROM Equipment").arg(col_readback));