Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MySQL Stored Procedure, accessing a single result set

MySQL Stored Procedure, accessing a single result set

Scheduled Pinned Locked Moved Solved General and Desktop
qsqlqueryqsqlquerymodelmysqlsql
3 Posts 2 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DaveK 0
    wrote on last edited by DaveK 0
    #1

    Hi,

    I am trying to run a stored procedure on a MySQL 5.6 database that returns a result set. Is it possible to access this result set in a QT Application?

    I see that the QSqlQuery class does not support MULTUPLE result sets. Does that mean that it does support calling a stored procedure that returns only a SINGLE result set? How would you access that result set since it is not the same as an "Output Variable"?

    Here is an example of a stored procedure I would like to access the result of in QT:

    USE `shipping`;
    DROP procedure IF EXISTS `getInventoryAvailableByWarehouse`;
    
    DELIMITER $$
    USE `shipping`$$
    CREATE PROCEDURE `getInventoryAvailableByWarehouse` ()
    BEGIN
    
        ## This is just an example to return a result set, the real stored procedure would have logic here.
        SELECT * FROM inventory;
    
    END$$
    
    DELIMITER ;
    
    

    What I would like to do in my Qt Application:

        QSqlQuery* qry = new QSqlQuery(m_db);
        QSqlQueryModel *model = new QSqlQueryModel;
    
        qry->prepare("CALL getInventoryAvailableByWarehouse ()");
        qry->exec();
        model->setQuery(*qry);
    
        ui->tableView->setModel(model);
    

    What I do not understand is how to get the result set from the stored procedure, if it is possible.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DaveK 0
      wrote on last edited by
      #2

      I found the answer:

      Instead of preparing the query, and then executing it. Just execute the query.

      THIS:

          qry->exec("CALL getInventoryAvailableByWarehouse ()");
          model->setQuery(*qry);
      

      NOT THIS:

          qry->prepare("CALL getInventoryAvailableByWarehouse ()");
          qry->exec();
          model->setQuery(*qry);
      
      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        There’s also no need to allocate the query on the heap as the QSqlQueryModel::setQuery method parameter suggests.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved