Results 1 to 4 of 4

Thread: Problem with calling stored procedures in QT

  1. #1
    Join Date
    Aug 2008
    Posts
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Problem with calling stored procedures in QT

    Hi,
    I am working on QT4.4 with using visual studio2005. Till now there is no problem with QT sql module but now I am facing a problem with QT while calling stored procedures.
    I am using MYSQL5.0 I connected to it properly and I am able to query the data from database schema .
    Now i created one stored procedure name "district( )" and I was trying to execute the stored procedure using QT code.

    Qt Code:
    1. QSqlQuery distquery;
    2. distquery.prepare("CALL District()");
    3. distquery.exec( );
    To copy to clipboard, switch view to plain text mode 

    if I execute the above code the result set is nothing I am getting. I tried in another way as

    Qt Code:
    1. QSqlQuery distquery;
    2. distquery.exec( "CALL District()");
    To copy to clipboard, switch view to plain text mode 

    Then it is working and I am able to get the result set.But I if I try as shown below

    Qt Code:
    1. QSqlQuery distquery("CALL District()");
    2. distquery.exec( );
    To copy to clipboard, switch view to plain text mode 

    It is showing a message like


    An unhandled win32 exception occurred in mysqld-nt.exe[584]
    and after this I am not able to connect with database even I recompile and run the applicatio.It is showing unnable to connect.I am very much confused what happenig if I try in this way , I tried in this way because I want to create a query with string and the database connection name also ie

    Qt Code:
    1. QSqluery query("CALL district()" , QSqlDatabase :: QSqlDataBase("name"));
    To copy to clipboard, switch view to plain text mode 

    I am very much confused with whethre it is a problem with QT or with my app.

    Please suggest me to solve this problem.

    Regards,
    Sudheer.

  2. #2
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Problem with calling stored procedures in QT

    Qt Code:
    1. QSqlQuery distquery;
    2. distquery.prepare("CALL District()");
    3. distquery.exec( );
    To copy to clipboard, switch view to plain text mode 

    This is nonsense, because you didn't have bounded variables. This call is only for prepared statements.

    Qt Code:
    1. QSqlQuery distquery("CALL District()");
    2. distquery.exec( );
    To copy to clipboard, switch view to plain text mode 

    Here you call the procuedure twice. because the following statement already excecute the procedure
    Qt Code:
    1. QSqlQuery distquery("CALL District()");
    To copy to clipboard, switch view to plain text mode 

    QSqlQuery::QSqlQuery ( const QString & query = QString(), QSqlDatabase db = QSqlDatabase() )

    Constructs a QSqlQuery object using the SQL query and the database db. If db is not specified, the application's default database is used. If query is not an empty string, it will be executed.
    I think what you want is the following:
    Qt Code:
    1. QSqlQuery distquery();
    2. distquery.exec("CALL District()" );
    To copy to clipboard, switch view to plain text mode 

    But read the documentation for more information.

    Best Regards
    NoRulez

  3. #3
    Join Date
    Aug 2008
    Posts
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem with calling stored procedures in QT

    Hi ,
    Thanks for quick and kind suggestion. I understood what you have suggested.But one thing I want to Constructs a QSqlQuery object using the SQL query and the database db and after next I want to execute it . How to write the line of code using the code below
    Qt Code:
    1. QSqlQuery distquery( );
    2. distquery.exec("CALL District( )" );
    To copy to clipboard, switch view to plain text mode 

    So please help me to solve this problem.

    Regards,
    sudheer.

  4. The following user says thank you to sudheer168 for this useful post:

    JazzKatua (4th January 2014)

  5. #4
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Problem with calling stored procedures in QT

    I don't know exactly what you mean, but here is a sample code:

    Qt Code:
    1. // Connect to a database
    2. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    3. db.setHostName("acidalia");
    4. db.setDatabaseName("customdb");
    5. db.setUserName("mojito");
    6. db.setPassword("J0a1m8");
    7. bool ok = db.open();
    8.  
    9. // Execute a SQL Statement
    10. QSqlQuery distquery;
    11. distquery.exec("CALL District()" );
    12.  
    13. // Fetch the result
    14. while(distquery.next()) {
    15. qDebug() << "Column 1: " << distquery.value(0);
    16. }
    To copy to clipboard, switch view to plain text mode 

    The following statement could also be modified:
    Qt Code:
    1. // Execute a SQL Statement
    2. QSqlQuery distquery;
    3. distquery.exec("CALL District()" );
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. // Execute a SQL Statement
    2. QSqlQuery distquery(db);
    3. distquery.exec("CALL District()" );
    To copy to clipboard, switch view to plain text mode 
    But the database connection is already used per default.


    See also http://doc.trolltech.com/4.5/qsqldatabase.html

    Best Regards

Similar Threads

  1. Problem calling stored procedure.. Help!
    By triperzonak in forum Qt Programming
    Replies: 4
    Last Post: 15th March 2010, 08:34
  2. Replies: 14
    Last Post: 16th January 2009, 08:11
  3. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  4. Stored procedure return values problem
    By jgreetham in forum Qt Programming
    Replies: 7
    Last Post: 10th September 2007, 17:38
  5. MySql Stored Procedures Woes
    By stevey in forum Qt Programming
    Replies: 9
    Last Post: 19th October 2006, 12:58

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.