Important: Please read the Qt Code of Conduct -

QSqlquery size returns -1 when forwardOnly is enabled

  • Hi,
    With a postgres database on linux with QPSQL driver, I cannot retrieve the number of results of a query with "size()" method, it always returns -1.

    bool hasSupport = QSqlDatabase::database().driver()->hasFeature(QSqlDriver::QuerySize);
    QSqlQuery query;
    query.setForwardOnly(true); // this prevents from getting nbRows
    query.exec("select 123");
    int nbRows = query.size();
    int val = query.value(0).toInt();
    "hasSupport => true"
    "nbRows => -1"
    "val => 123"

    I guess that when using QPSQL driver, setForwardOnly() is incompatible with size()?
    I didn't see it documented anywhere

  • Lifetime Qt Champion

    ForwardOnly queries can't return the complete query size.

Log in to reply