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

QSqlDatabase and QSqlQuery



  • I am implementing database connectivity between a C++ application and a script. The script calls a function in the C++ application to connect to the database, this works and the database connection is established.

    The script attempts to perform a query on the database via the C++ application. I'm not sure how to implement error handling correctly at the moment. The query is very simple:

     SELECT * FROM tblTest
    

    This doesn't appear to return any rows, however if I perform the same query using HeidiSQL on the database, one record is returned.

    How can I tell if there is a problem with the query in the C++ application?


  • Lifetime Qt Champion

    @SPlatten
    Hi
    last error is a god starting point
    https://doc.qt.io/qt-5/qsqlquery.html#lastError
    and output its https://doc.qt.io/qt-5/qsqlerror.html#text
    often gives clues if anything it didn't like.

    Also , its important you use next() correctly.

     QSqlQuery query("SELECT * from person ");
        while (query.next()) {
        ..use values
      }
    

Log in to reply