PDA

View Full Version : QSqlQuery return result



arpspatel
9th April 2010, 02:02
Hi,



QString QueryDatabase(QString _query){
QSqlDatabase db = QSqlDatabase::addDatabase(type); //Available Types QMYSQL, QPSQL and QSQLITE
db.setDatabaseName(name);
db.setHostName(host);
if(!db.open(user,pass)){
return -1;
}

QSqlQuery query;
query.exec(_query);
QString result = query.result(); //How do i do this part
db.close();
return result;
}



I want to query the database with string like "select count(*)....." and the return value will always be in one row as its just counting the number of occurances.. how can i get the result direct of the row to QString??

Thanks

norobro
9th April 2010, 02:45
Take a look at the docs (http://doc.qt.nokia.com/4.6/qsqlquery.html) under "Detailed Description"

toutarrive
9th April 2010, 07:55
QString result = query.result();
query.result() will return a pointer on a const QSqlResult . You statement can not work.

According to Qt doc:


Normally, you would use QSqlQuery instead of QSqlResult, since QSqlQuery provides a generic wrapper for database-specific implementations of QSqlResult.

If you are implementing your own SQL driver (by subclassing QSqlDriver), you will need to provide your own QSqlResult subclass that implements all the pure virtual functions and other virtual functions that you need.


Retrieving records:

Once an active query is positioned on a valid record, data can be retrieved using value(). All data is transferred from the SQL backend using QVariants.


QSqlQuery query("SELECT * FROM country");
while (query.next()) {
QString country = query.value(0).toString();
doSomething(country);
}