PDA

View Full Version : QSqlQuery, bindValue and Sqlite



cydside
5th April 2009, 15:57
Hi to all,
I'm having some problems binding values in a "SELECT" statement on a table, maybe the code will explain better:



void frmLogin::submit()
{
QString strQuery;
QString strName;
QString strPass;

strQuery = "SELECT * FROM tbUtente WHERE utenteName=':name';"; //doesn't work
//strQuery = "SELECT * FROM tbUtente;"; //it works
strName = ui.lneUser->text();
strPass = ui.lnePwd->text();
/*
qDebug() << "Nome: " << strName;
qDebug() << "Pass: " << strPass;
*/

QSqlQuery queryUser;
queryUser.prepare(strQuery);
queryUser.bindValue(":name", strName); //doesn't work
// without bind a value it works
queryUser.exec();

while (queryUser.next())
{
qDebug() << "Nome: " << queryUser.value(1).toString();
qDebug() << "Pass: " << queryUser.value(2).toString();
}

QSqlRecord rec = queryUser.record();

if (rec.count() >= 1)
{
this->accept();
qDebug() << "Ok submit!";
}
else
{

ui.lneUser->clear();
ui.lnePwd->clear();
ui.lneUser->setFocus();
}

userName = ui.lneUser->text();
}


so, if I use the "//doesn't work" instrutcions (line 7 and line 18) it doesn't return a value, instead, if I comment line 7 and 18 and run line 8 it returns all the values!!!
Why it happens?!

thanks in advance
PS: I'm using Qt 4.5.0, Sqlite3 database and WinXP.

Grimlock
5th April 2009, 16:40
Try it like this:


strQuery = "SELECT * FROM tbUtente WHERE utenteName = :name"


no ' Qt takes care of that for You.

talk2amulya
5th April 2009, 16:43
try removing single quotes around :name in strQuery

cydside
5th April 2009, 16:44
Thank you!!!!!

Grimlock
5th April 2009, 16:53
You're welcome :cool: