mardi 5 avril 2016

Why are decimals dropped in my Qt App in std::vector

In my Qt application I use a C++ function that fills a std::vector<double> with data from a MySQL database. The function uses MySQL Connector/C++ and does the following:

void create_price(sql::Connection *con, std::string database, std::string table, std::vector<double> &price_vec)
{
        sql::Statement *stmt;
        sql::ResultSet *res;

        std::string use_database = "USE " + database;

        stmt = con->createStatement();
        stmt->execute(use_database);

        use_database = "SELECT Time,LastVolume,LastPrice FROM "+ table +" WHERE LastVolume >0 OR LastPrice >0 ORDER BY Time ASC";

        res = stmt->executeQuery(use_database); //will return many rows

        while (res->next()) {         
                price_vec.push_back(res->getDouble("LastPrice")); 
        }
}

When I call this function with any std::vector<double> in my plain C++ application I get flawless results. However, when I call this function in my Qt application the passed vector is filled with ints. The decimal part is dropped!

It seems to be an issue with the MySQL part, since this test function returns proper doubles, even when called from within my Qt application.

void double_test(std::vector &doublevec){ 
    //just some doubles to test.
    std::vector<double> doublevec;
    doublevec.push_back(1.4);
    doublevec.push_back(5.324);
}

Is it possible that the double returned by the MySQL query is not actually a real double and therefore not compatible with Qt?

Any hint on what I can do to understand the problem better would also help.

Aucun commentaire:

Enregistrer un commentaire