Results 1 to 3 of 3

Thread: QSqlQuery return result

  1. #1
    Join Date
    Apr 2008
    Posts
    35
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlQuery return result

    Hi,

    Qt Code:
    1. QString QueryDatabase(QString _query){
    2. QSqlDatabase db = QSqlDatabase::addDatabase(type); //Available Types QMYSQL, QPSQL and QSQLITE
    3. db.setDatabaseName(name);
    4. db.setHostName(host);
    5. if(!db.open(user,pass)){
    6. return -1;
    7. }
    8.  
    9. QSqlQuery query;
    10. query.exec(_query);
    11. QString result = query.result(); //How do i do this part
    12. db.close();
    13. return result;
    14. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QSqlQuery return result

    Take a look at the docs under "Detailed Description"

  3. #3
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery return result

    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.
    Qt Code:
    1. QSqlQuery query("SELECT * FROM country");
    2. while (query.next()) {
    3. QString country = query.value(0).toString();
    4. doSomething(country);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by toutarrive; 9th April 2010 at 08:15. Reason: updated

Similar Threads

  1. Replies: 11
    Last Post: 11th April 2014, 14:11
  2. Replies: 1
    Last Post: 17th January 2010, 07:36
  3. QColorDialog always return 0 as a result
    By NoRulez in forum Qt Programming
    Replies: 8
    Last Post: 17th October 2009, 12:06
  4. Wrong compiling result by QT!!??
    By greenoaktree in forum Qt Programming
    Replies: 2
    Last Post: 13th January 2008, 13:36
  5. QSqlQuery always return junk value while for varchar oracle.
    By ranjit2709 in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2007, 05:19

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.