Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

{SOLVED} QSql obtain a specific value from the table



  • QSql is a news for me ......
    But I open correctly my db (with error test), insert correcly my data, update correctly too .... but have some problem with these piece of code:

    @ int p = 0;
    QSqlQuery qy(db);
    qy.prepare("SELECT id_pro1 FROM program WHERE pro1 = :pro1)");
    qy.bindValue(":pro1", "ffff");
    qy.exec();
    qy.next();
    p = qy.record().toInt();
    ui->lcdNumber_2->display(p);
    qy.clear();@

    I tray some method ( .value, .arg) but obtain only p=0 ..... I dont Understand why.



  • Hi gfxx.
    Try using @p = qy.value(0).toInt();
    @
    (based on "QSqlQuery::record()":http://qt-project.org/doc/qt-5.0/qtsql/qsqlquery.html#record )

    Insteads of @p = qy.record().toInt();
    @

    Regards.



  • Hi,

    line 7 of your code is WRONG; chage it with
    @
    p = qy.value (0).toInt();
    @

    to access to Query result you must use QSqlQuery::value(), QSqlQuery::record() is used to inspect result structure (column count, data types, ....)



  • tanks for the answer .... but code return only
    @p = 0@
    ..... but in table id_pro1 value are:
    @ id_pro1 = 28@

    I have other update and insert in these void .... and all work good ..... I have only one select in these file and not work properly ......

    Some idea??



  • MY BIG MISTAKE ......

    in my code I have added these line at the end:

    @qDebug() << p;@

    and obtain these messages ....

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

    If control my code at the line 3 .....

    @ qy.prepare("SELECT id_pro1 FROM program WHERE pro1 = :pro1)");@

    the ")" caracters is a mistake .... now the code work perfect

    Tank you at all.


Log in to reply