Results 1 to 7 of 7

Thread: QODBC and stored procedures

  1. #1
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QODBC and stored procedures

    Hi all,

    I'm connecting with QODBC to a SQL Server Database; I have a stored procedure that returns a value
    How can I get this value?

    Thanks

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QODBC and stored procedures

    This is the simpliest way, I think.

    Qt Code:
    1. //declare you QSqlQuery
    2. QString qstr = QString("SELECT your_stored_proc(<input params>);");
    3.  
    4.  
    5. if ( !query.exec(qstr) ) {
    6. // handle this
    7. }
    8.  
    9. // if nothing has been returned
    10. if ( !query.first() ) {
    11. // handle this
    12. }
    13.  
    14. //let us assume that your stored proc returns integer
    15. int var = query.value(0).toInt();
    To copy to clipboard, switch view to plain text mode 
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QODBC and stored procedures

    With this query:
    Qt Code:
    1. QString stmt="SELECT [Login](:result, :type, :username, :password);";
    To copy to clipboard, switch view to plain text mode 

    I get this error:
    [Microsoft][SQL Native Client][SQL Server]Incorrect syntax near '@P1'. [Microsoft][SQL Native Client][SQL Server]Statement(s) could not be prepared. QODBC3: Unable to execute statement"

  4. #4
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QODBC and stored procedures

    Did you bind your paramter values?
    Could you show your code?
    I'm a rebel in the S.D.G.

  5. #5
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QODBC and stored procedures

    This is my code

    Qt Code:
    1. ConnectDialog::LoginResult ConnectDialog::login()
    2. {
    3. QString stmt="SELECT [Login](:result, :type, :username, :password);";
    4.  
    5. q.setForwardOnly(true);
    6. md5=QCryptographicHash::hash(password().toLatin1(),
    7. QCryptographicHash::Md5);
    8. q.prepare(stmt);
    9. q.bindValue(":result", QVariant(0), QSql::InOut);
    10. q.bindValue(":type", QVariant(0), QSql::InOut);
    11. q.bindValue(":username", userName());
    12. q.bindValue(":password", md5);
    13. if(!q.exec())
    14. {
    15. qCritical()<<q.lastError().text();
    16. }
    17. else if(!q.first())
    18. {
    19. qCritical()<<q.lastError().text();
    20. }
    21. qDebug()<<q.record();
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QODBC and stored procedures

    I don't have such an opportunity to compile code, but nevertheless... don't you need to put username and password in quotes(or double-quotes)?
    I'm using PostgreSQL and it requires quotes around text/varchar/char params.
    I'm a rebel in the S.D.G.

  7. #7
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QODBC and stored procedures

    No, because is a prepared query, QSqlQuery escape it.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.