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. calling stored procedure in qt
Qt 6.11 is out! See what's new in the release blog

calling stored procedure in qt

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 6.6k Views 2 Watching
  • 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.
  • titanT Offline
    titanT Offline
    titan
    wrote on last edited by titan
    #1

    hi
    I wrote below program to connect a stored procedure in qt:

    if(db.open())
    {
    QString testStr;

        testStr.reserve( 100 );
    
        QSqlQuery query;
        query.setForwardOnly(true);
    
        query.prepare("{CALL getTestString(:testStr)}");
        query.bindValue("testStr", testStr, QSql::Out);
        if( query.exec() )
        {
            testStr = query.boundValue("testStr").toString();
            if( testStr.isEmpty() )
                qDebug() << "String is empty"; //always return empty string
            else
                qDebug() << testStr;
    
    
        }
        else
        {
            qDebug() << query.lastError().text();
        }
    }
    else
    {
        qDebug() << "Can't connect";
    }
    

    and this is the stored procedure :

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE getTestString
    @key nvarchar(50) OUTPUT
    AS
    BEGIN
    SET NOCOUNT ON;
    SET @key = 'TEST'
    END
    GO

    when I execute the program, it always returns:
    String is empty
    what's the problem?

    1 Reply Last reply
    0
    • mrdebugM Offline
      mrdebugM Offline
      mrdebug
      wrote on last edited by
      #2

      Have you verify your stored procedure with a sql editor?

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • titanT Offline
        titanT Offline
        titan
        wrote on last edited by
        #3

        yes I used sqleditor,
        I could solve it, the problem was in the qt program.
        thanks anyway

        mrjjM 1 Reply Last reply
        0
        • titanT titan

          yes I used sqleditor,
          I could solve it, the problem was in the qt program.
          thanks anyway

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @titan
          Could you make as solved then and if possible
          show the code that did work?

          titanT 1 Reply Last reply
          0
          • mrjjM mrjj

            @titan
            Could you make as solved then and if possible
            show the code that did work?

            titanT Offline
            titanT Offline
            titan
            wrote on last edited by kshegunov
            #5

            @mrjj

            if(db.open())
                {
                    QString testStr ="*****";
            
                    testStr.reserve( 100 );
            
                    QSqlQuery query;
                    query.setForwardOnly(true);
            
                    query.prepare("{CALL getTestString(:testStr)}");
                    query.bindValue(":testStr", testStr, QSql::Out);
                    if( query.exec() )
                    {
                        testStr = query.boundValue(":testStr").toString();
                        if( testStr.isEmpty() )
                            qDebug() << "String is empty";
                        else
                            qDebug() << testStr;
            
            
                    }
                    else
                    {
                        qDebug() << query.lastError().text();
                    }
                }
                else
                {
                    qDebug() << "Can't connect";
                }
            
            1 Reply Last reply
            2
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Thank you :)

              titanT 1 Reply Last reply
              0
              • mrjjM mrjj

                Thank you :)

                titanT Offline
                titanT Offline
                titan
                wrote on last edited by
                #7

                @mrjj
                you're welcome :)

                1 Reply Last reply
                0

                • Login

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