calling stored procedure in qt



  • 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?



  • Have you verify your stored procedure with a sql editor?



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


  • Qt Champions 2016

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



  • @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";
        }
    

  • Qt Champions 2016

    Thank you :)



  • @mrjj
    you're welcome :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.