PDA

View Full Version : Maybe a bug: Reading double precision columns from a PostgreSQL Table



Sparhawk
24th July 2008, 18:35
Hi,

Let a table on my PostgreSQL 7.4 database.

CREATE TABLE foo
(id serial,
coste double precision,
pvp double precision)

Let insert a row
INSERT INTO foo (coste, pvp) VALUES (0.12345, 1.98765).

Let next code



#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QVariant>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

if ( QSqlDatabase::isDriverAvailable("QPSQL") ) {
QSqlDatabase db = QSqlDatabase::addDatabase( "QPSQL" ) ;
db.setDatabaseName( "mydatabase" ) ;
db.setHostName( "localhost" ) ;
db.setUserName( "myuser");
db.setPassword( "mypassword" ) ;
if ( !db.open() ) {
qDebug() << "Failed opening database dbToConnectTo\nReason:" ;
qDebug() << db.lastError().driverText();
qDebug ()<< db.lastError().databaseText() ;
exit ( 1 ) ;
} else {
QSqlQuery qry(db);
qry.prepare("select coste, pvp from foo where id=621");
if ( qry.exec() ) {
qry.first();
qDebug() << qry.value(0).toDouble();
qDebug() << qry.value(1).toDouble();
}
}
}
}


On the output, I expect to see

-----------------------------------------
0.12345
1.98765
-----------------------------------------

But, what I get is

-----------------------------------------
0
1
-----------------------------------------

Is that a bug? Can you test it? Maybe it's an error of my code. If it's not, I'll report the bug to Trolltech.

Thanks!!!

Sparhawk
24th July 2008, 19:20
I found a workaround... Using a cast on the postgresql query to varchar:

select cast(coste as varchar), cast(pvp as varchar) from foo ...

Now it works.