PDA

View Full Version : Problem with calling stored procedures in QT



sudheer168
9th November 2009, 07:30
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.




QSqlQuery distquery;
distquery.prepare("CALL District()");
distquery.exec( );



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




QSqlQuery distquery;
distquery.exec( "CALL District()");



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




QSqlQuery distquery("CALL District()");
distquery.exec( );



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




QSqluery query("CALL district()" , QSqlDatabase :: QSqlDataBase("name"));



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.

NoRulez
9th November 2009, 08:02
QSqlQuery distquery;
distquery.prepare("CALL District()");
distquery.exec( );


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



QSqlQuery distquery("CALL District()");
distquery.exec( );


Here you call the procuedure twice. because the following statement already excecute the procedure


QSqlQuery distquery("CALL District()");


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:


QSqlQuery distquery();
distquery.exec("CALL District()" );


But read the documentation (http://doc.trolltech.com/4.5/qsqlquery.html#QSqlQuery-2) for more information.

Best Regards
NoRulez

sudheer168
9th November 2009, 08:47
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



QSqlQuery distquery( );
distquery.exec("CALL District( )" );



So please help me to solve this problem.

Regards,
sudheer.

NoRulez
9th November 2009, 09:07
I don't know exactly what you mean, but here is a sample code:



// Connect to a database
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("acidalia");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");
bool ok = db.open();

// Execute a SQL Statement
QSqlQuery distquery;
distquery.exec("CALL District()" );

// Fetch the result
while(distquery.next()) {
qDebug() << "Column 1: " << distquery.value(0);
}


The following statement could also be modified:

// Execute a SQL Statement
QSqlQuery distquery;
distquery.exec("CALL District()" );

to

// Execute a SQL Statement
QSqlQuery distquery(db);
distquery.exec("CALL District()" );

But the database connection is already used per default.


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

Best Regards