PDA

View Full Version : MySQL Floatingpoint zero value



sp3x
5th June 2016, 20:52
Hi everyone,

everytime I try to get a Floatingpoint value from my MySQL Database QSqlQuery.value(X) returns a invalid value.
toFloat() returns just zero.

If the value stored in the MySQL isnt with floatingpoint, QSqlQuery.value() returns valid data.

Why I get only invalid values if I try to get Data from the MySQL database? Everything is working, QString, Integers, Binary, but float, double, real isnt working!

Please help.

Regards Marcel

anda_skoa
5th June 2016, 21:23
What type do you get when you look at the value returned b QSqlQuery::value()?

Cheers,
_

sp3x
5th June 2016, 21:43
Hi,

It returns "Invalid".

But only if I get a floatingpoint value. If the value stored in mysql is a decimal value i.e. 24 there is no error. If i change the value to 24.12 then it retunrs "invalid".

The type of the mysql field is double. I also tested it with float.

sp3x
6th June 2016, 16:21
Here is an example:


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setUserName("dtf");
db.setPassword("dtf");
db.setDatabaseName("dtf");

if(!db.open()) {
qDebug() << db.lastError();
return;
}

QSqlQuery sql;
sql.prepare("SELECT result FROM test WHERE id = 1;");

if(!sql.exec()) {
qDebug() << db.lastError();
return;
}

sql.next();
qDebug() << "Result: " << sql.value(0).toDouble();

The output of this example is:

Result: 0

The MySQL column "result" is of type DOUBLE(10,2). If i change the type to DOUBLE() only, the result is the same.
The data selected by the statement is "14.99".

Its an absolute simple example. Why this wont work?

Added after 23 minutes:

Ive tested it without prepared-statements! And now it works! Is it a BUG in QT5?

Thats working:

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setUserName("dtf");
db.setPassword("dtf");
db.setDatabaseName("dtf");

if(!db.open()) {
qDebug() << db.lastError();
return;
}

QSqlQuery sql;

if(!sql.exec("SELECT result FROM test WHERE id = 1;")) {
qDebug() << db.lastError();
return;
}

sql.next();
qDebug() << "Result: " << sql.value(0).toDouble();