[Solved] Qt problem with query in SQLite database in Qt
-
Hi,
I am facing this problem:@QString name, passwd;
name=ui->nom->text();
passwd=ui->motdepasse->text();
if(!mydb.isOpen())
{
ui->label_2->setText("pas de connexion");
//qDebug()<<"Failed to open my database";
//return;
}
QSqlQuery qry;
if(qry.exec("SELECT * FROM emploi WHERE ref='"+passwd+"' AND entreprise='"+name+"'"))
{
int count=0;
while(qry.next())
{
count++;
}
if(count==1)ui->label_2->setText("username and passwd are correct"); else if(count>1) ui->label_2->setText("duplication"); else ui->label_2->setText("this count does not exist"); } else ui->label_2->setText("errors");
}
@
When running I get the message "errors" that mean the query exec return false.
Also when I replace the previous query with @ if(qry.exec("SELECT * FROM emploi WHERE ref=0 AND entreprise='alcatel'"))@
the program run correctly and poster this message "username and passwd are correct"(I have it in my table emploi).
Can someone help me and thank you. -
Hi,
I suggest to use qry.bindValue instead of putting your variables directly into QString, "see here: ":http://qt-project.org/doc/qt-5/qsqlquery.html#bindValue-2
Your problem seems to be related to the field "ref" which seems to be integer and you want to pass a string-value. -
You may try this:
@
...
QSqlQuery qry;
qry.prepare("SELECT * FROM emploi WHERE ref = :pref AND entreprise= :pent;")
qry.bindValue(":pref", passwd);
qry.bindValue(":pent", name);if (!qry.exec()) {
qDebug() << qry.lastError(); // to see what's the problem
} else {
int count=0;
while(qry.next())
...
}
@