query prepare with sql math
This is my code:
Code:
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
Re: query prepare with sql math
What do query.exec() and query.lastError() return?
Re: query prepare with sql math
code:
Code:
query.prepare("select :a / :b");
query.addBindValue( ui.a_lineEdit->text() );
query.addBindValue( ui.b_lineEdit->text() );
query.exec();
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
Re: query prepare with sql math
exec() returns true or false. if exec() returns false that means that an error appeared during query executing.
Re: query prepare with sql math
Code:
query.prepare("select :a / :b");
query.addBindValue( ui.a_lineEdit->text() );
query.addBindValue( ui.b_lineEdit->text() );
if (!query.exec()){
QString("There is an error with query.exec()!"));
}else{
query.exec();
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
Re: query prepare with sql math
Re: query prepare with sql math
what query.exec() and query.lastError() returns?
Re: query prepare with sql math
this code returns "2"
Code:
db.setHostName("localhost");
db.setDatabaseName("test.db");
db.setUserName("system");
db.setPassword("sysdba");
if (!db.open())
qWarning() << db.lastError().text();
else {
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);
}
}
Re: query prepare with sql math
I have tested your code and it returns "2".
Something strange is happening, because when I change
Code:
query.prepare("select :a / :b");
to
Code:
query.prepare("select :a + :b");
or
Code:
query.prepare("select :a - :b");
or
Code:
query.prepare("select :a * :b");
it returns me the right result.
Do you see anything wrong with my code?
Renan
Re: query prepare with sql math
what kind of database do you use? mysql, right?
Re: query prepare with sql math
yes mysql.
here is the the pushbutton code:
Code:
void testeMYSQL2::on_soma_pushButton_clicked()
{
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()){
QString("The error:\n%1").
arg(db.
lastError().
text()));
}
else{
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()){
QString("There is an error with query.exec()!"));
}else{
// query.exec();
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
Re: query prepare with sql math
Here is something really strange:
if I change:
Code:
query.prepare("SELECT :a / :b");
to
Code:
query.prepare("SELECT (:a / :b)*1");
I get the result.
Maybe a QT bug?
Renan
Re: query prepare with sql math
Indeed it behaves strange. Could you prepare a minimal compilable example?
5 Attachment(s)
Re: query prepare with sql math
I have uploaded a simple compilable example, as jacek asked.
Renan
I have uploaded it again.
Re: query prepare with sql math
I can't download testemysql2.cpp
Re: query prepare with sql math
I have uploaded it again.
Renan
Re: query prepare with sql math
looks like bug in Qt.
this code works, you can use it
Code:
...
query.
prepare(QString("select %1/%2").
arg(ui.
lineEdit->text
()).
arg(ui.
lineEdit_2->text
()));
...
Re: query prepare with sql math
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
Re: query prepare with sql math
I've tested on Qt 4.4.1, the result is the same.
1 Attachment(s)
Re: query prepare with sql math
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:
Code:
query.addBindValue( 4.0 );
query.addBindValue( 2.0 );