View Full Version : query prepare with sql math
GuL
29th August 2008, 15:33
This is my code:
QSqlQuery query;
query.prepare("select :a / :b");
query.addBindValue( ui.a_lineEdit->text() );
query.addBindValue( ui.b_lineEdit->text() );
query.exec();
while (query.next()) {
QString res = query.value(0).toString();
ui.res_lineEdit->setText(res);
ui.a_lineEdit->text() is 9 and ui.b_lineEdit->text() is 3
If I try to +,- or * the result shows up in ui.res_lineEdit->setText(res), but when I try to divide it doesnt show me anything. Why?
Renan
jacek
29th August 2008, 15:56
What do query.exec() and query.lastError() return?
GuL
1st September 2008, 14:07
code:
QSqlQuery query;
query.prepare("select :a / :b");
query.addBindValue( ui.a_lineEdit->text() );
query.addBindValue( ui.b_lineEdit->text() );
query.exec();
QMessageBox::critical(0, tr("Error"),
QString("The error:\n%1").arg(query.lastError().text()));
while (query.next()) {
QString res = query.value(0).toString();
ui.res_lineEdit->setText(res);
query.lastError() returns me nothing.
What could I do to know what query.exec() returns?
Renan
spirit
1st September 2008, 14:14
exec() returns true or false. if exec() returns false that means that an error appeared during query executing.
GuL
1st September 2008, 15:44
QSqlQuery query;
query.prepare("select :a / :b");
query.addBindValue( ui.a_lineEdit->text() );
query.addBindValue( ui.b_lineEdit->text() );
if (!query.exec()){
QMessageBox::critical(0, tr("Error"),
QString("There is an error with query.exec()!"));
}else{
query.exec();
QMessageBox::critical(0, tr("Error"),
QString("There is no error with query.exec():\n%1").arg(query.lastError().text()));
while (query.next()) {
QString res = query.value(0).toString();
ui.res_lineEdit->setText(res);
}
There is no error with query.exec() and query.lastError() returns me nothing.
Any suggestions?
Renan
spirit
1st September 2008, 16:01
sorry, it works.
GuL
1st September 2008, 16:24
what query.exec() and query.lastError() returns?
spirit
1st September 2008, 16:27
this code returns "2"
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setHostName("localhost");
db.setDatabaseName("test.db");
db.setUserName("system");
db.setPassword("sysdba");
if (!db.open())
qWarning() << db.lastError().text();
else {
QSqlQuery query(db);
query.prepare("CREATE TABLE test (a int, b int)");
if (!query.exec())
qWarning() << query.lastError().text();
else {
qDebug() << query.exec("INSERT INTO test (a, b) VALUES (1, 2)");
qDebug() << query.exec("INSERT INTO test (a, b) VALUES (3, 4)");
qDebug() << query.exec("INSERT INTO test (a, b) VALUES (5, 5)");
query.prepare("SELECT :a / :b");
query.addBindValue(4);
query.addBindValue(2);
qDebug() << query.exec();
query.next();
qDebug() << query.value(0);
}
}
GuL
1st September 2008, 19:01
I have tested your code and it returns "2".
Something strange is happening, because when I change
query.prepare("select :a / :b");
to
query.prepare("select :a + :b");
or
query.prepare("select :a - :b");
or
query.prepare("select :a * :b");
it returns me the right result.
Do you see anything wrong with my code?
Renan
spirit
1st September 2008, 19:07
what kind of database do you use? mysql, right?
GuL
1st September 2008, 19:25
yes mysql.
here is the the pushbutton code:
void testeMYSQL2::on_soma_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(ui.hostname_lineEdit->text());
db.setDatabaseName(ui.database_lineEdit->text());
db.setUserName(ui.username_lineEdit->text());
db.setPassword(ui.password_lineEdit->text());
db.open();
if (!db.open()){
QMessageBox::critical(0, tr("Error"),
QString("The error:\n%1").arg(db.lastError().text()));
}
else{
QSqlQuery query;
query.prepare("SELECT :a / :b");
// query.addBindValue( ui.a_lineEdit->text() );
// query.addBindValue( ui.b_lineEdit->text() );
// query.addBindValue( 400 );
// query.addBindValue( 2 );
query.bindValue(":a", ui.a_lineEdit->text() );
query.bindValue(":b", ui.b_lineEdit->text() );
if (!query.exec()){
QMessageBox::critical(0, tr("Error"),
QString("There is an error with query.exec()!"));
}else{
// query.exec();
QMessageBox::critical(0, tr("Error"),
QString("There is NO error with query.exec():\n%1").arg(query.lastError().text()));
while (query.next()) {
QString res = query.value(0).toString();
ui.res_lineEdit->setText(res);
}
}
}
db.close();
}
Renan
GuL
1st September 2008, 20:16
Here is something really strange:
if I change:
query.prepare("SELECT :a / :b");
to
query.prepare("SELECT (:a / :b)*1");
I get the result.
Maybe a QT bug?
Renan
jacek
1st September 2008, 22:11
Indeed it behaves strange. Could you prepare a minimal compilable example?
GuL
2nd September 2008, 14:45
I have uploaded a simple compilable example, as jacek asked.
Renan
I have uploaded it again.
spirit
2nd September 2008, 14:51
I can't download testemysql2.cpp
GuL
2nd September 2008, 15:01
I have uploaded it again.
Renan
spirit
2nd September 2008, 15:44
looks like bug in Qt.
this code works, you can use it
...
query.prepare(QString("select %1/%2").arg(ui.lineEdit->text()).arg(ui.lineEdit_2->text()));
...
GuL
2nd September 2008, 16:45
Thanks again spirit.
I will wait one or two days to report these to trolltech. Maybe someone knows why this is happening.
I'm using QT 4.3.4 open source Linux Ubuntu 8.04. I don't if this happens with QT 4.4.1.
Renan
spirit
2nd September 2008, 16:49
I've tested on Qt 4.4.1, the result is the same.
jacek
2nd September 2008, 21:41
I've minimized it a bit further (see attachment) and it appears that for Qt 4.4.1 on Linux I get the "No results." message, which means that the database doesn't return any data. Of course it works for * operator.
I've also found a workaround:
query.addBindValue( 4.0 );
query.addBindValue( 2.0 );
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.