Frage

I'm using qt 4.8 and psql driver to connect to a postgres 9.1 database.

I'm doing a generic library to connect and insert into the database; almost all methods are ready but in the last one, I need to do a select from a table to insert values into another. When I try the select statement it behaves different. According to the code in turn but no one of the tests I made have resulted in a correct solution. Here's my code:

struct enfriadores enf;
enf.horaE=time(&enf.horaE);
enf.horaS=time(&enf.horaS)+1900;

  //base1.insertaEvento(db,enf);

QString consulta = "Select id_evento from eventos where extract(epoch from horae)=:hora_bus";

QDateTime hora_bus(QDateTime::fromTime_t(enf.horaE));

//qDebug()<< enf.horaE;

QSqlQuery query(db);
query.prepare(consulta);                                               
query.bindValue(":hora_bus",hora_bus);
query.exec();
query.first();

while(query.next())
{

    int valor = query.value(0).toInt();
    qDebug() << valor << endl;
}

The base1.insertaEvento is a call from a class I did to insert data on the table where afterwards I'll need to extract the id. The

qDebug() << enf.horaE;

I put it to know if the time was in the right form before I attached it to the query, which by the way, was correct. horaE is taken from a struct I have declaed in the previously mentioned class.

When I run the query as it is with the while(query.next()) it runs good but returns no results and if I delete the while loop but still maintain the query.next() compiler returns

QSqlQuery::value: not positioned on a valid record
0

I tried using the query.first() method and the query.setForwardOnly(true) but with same results.

Also if I try the value of hora_bus with qDebug() and replace it directly in the psql console I get a positive match so the problem is not in the way data is inserted or formatted, it's in the way the query is retrieved I believe but do not know how to resolve this Any ideas? Thanks

War es hilfreich?

Lösung

The SQL expression extract(epoch from horae) produces a number of seconds since 1/1/1970 so so that's what should be passed to the parameter :hora_bus.

The QDateTime::fromTime_t(enf.horaE) indicates that enf.horaE has this value, however instead of passing ot to the query, it's passing a QDateTime object whose text representation is probably going to be a string with year,month,etc... that can't be compared to a number of seconds.

So try this instead:

query.bindValue(":hora_bus",enf.horaE);

Also the code shouldn't ignore the boolean return values of prepare() and exec(). You don't want to try looping within results when the execution of the query has failed.

EDIT1: indeed when passing a QDateTime set to today to a prepared query similar to yours, QSqlQuery::exec() returns false with an SQL error invalid input syntax for type double precision.

EDIT2: it appears QVariant doesn't support being initialized with a long so an explicit cast to a different supported type is necessary. I've chosen qlonglong for a safe larger value:

query.bindValue(":hora_bus",(qlonglong)enf.horaE);

Tested, it worked for me.

Andere Tipps

http://qt-project.org/doc/qt-4.8/qsqlquery.html#details

At the very end of the documentation it mentions the following:

Warning: You must load the SQL driver and open the connection before a QSqlQuery is created. Also, the connection must remain open while the query exists; otherwise, the behavior of QSqlQuery is undefined.

If the connection to the database is timing out, or one of the variables you are using goes out of scope, you might disconnect early and get undefined results.

You can also check the return values on most of your function calls to see if they were successful or not.

Hope that helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top