Results 1 to 20 of 21

Thread: Complex query in QSqlQuery

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Complex query in QSqlQuery

    Do you use QSqlQuery::prepare()?

  2. #2
    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: Complex query in QSqlQuery

    Qt Code:
    1. bool ReportPrinter::chargeReport(const QDate &beginDate, const QDate &endDate){
    2. const QString file=":/database/chargereport.sql";
    3. QString sql;
    4.  
    5. if(!loadFile(file, &sql)) return false;
    6. QSqlQuery query;
    7.  
    8. query.prepare(sql);
    9. query.bindValue(":beginDate", beginDate);
    10. query.bindValue(":endDate", endDate);
    11. if(!query.exec()){
    12. QMessageBox::critical(0, qApp->applicationName(), query.lastError().text());
    13. return false;
    14. }
    15. else while(query.next()){
    16. qDebug()<<query.value(0)<<query.value(1)<<query.value(2)<<query.value(3);
    17. }
    18.  
    19. return true;
    20. }
    To copy to clipboard, switch view to plain text mode 

    Sure this is my source code

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Complex query in QSqlQuery

    What happens if you don't use prepare()?

  4. #4
    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: Complex query in QSqlQuery

    Quote Originally Posted by wysota View Post
    What happens if you don't use prepare()?
    If I don't use prepare I can't bind the values... But I will try removing the bind values

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Complex query in QSqlQuery

    You can use QString::arg() to bind values.

  6. #6
    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: Complex query in QSqlQuery

    Qt Code:
    1. bool ReportPrinter::chargeReport(const QDate &beginDate, const QDate &endDate){
    2. const QString file=":/database/chargereport.sql";
    3. QString sql;
    4.  
    5. if(!loadFile(file, &sql)) return false;
    6. QSqlQuery query;
    7.  
    8. /* query.prepare(sql);
    9. query.bindValue(":beginDate", beginDate);
    10. query.bindValue(":endDate", endDate);*/
    11. if(!query.exec(sql)){
    12. QMessageBox::critical(0, qApp->applicationName(), query.lastError().text());
    13. return false;
    14. }
    15. else while(query.next()){
    16. qDebug()<<query.value(0)<<query.value(1)<<query.value(2)<<query.value(3);
    17. }
    18.  
    19. return true;
    20. }
    To copy to clipboard, switch view to plain text mode 

    sql Code:
    1. SELECT @m:=(
    2. SELECT COUNT(cardid)
    3. FROM `transactions`
    4. WHERE
    5. operation>=1000 AND
    6. operation<2000 );
    7.  
    8. SELECT
    9. @m:=@m+1 AS 'id',
    10. t.date,
    11. o.name,
    12. t.amount
    13. FROM `transactions` AS t
    14. INNER JOIN `operations` AS o ON t.operation=o.id
    15. WHERE
    16. operation>=1000 && operation<2000
    17. ORDER BY date;
    To copy to clipboard, switch view to plain text mode 

    It gives to me the result of the first SELECT. I need the the result of 2nd SELECT
    Last edited by xgoan; 14th March 2008 at 12:01.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Complex query in QSqlQuery

    Does it work when you issue the same statement using mysql console? Are variables part of the sql specification or a feature of the console?

    How about modifying the statement so that you use a subquery instead of the variable assignment?

  8. #8
    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: Complex query in QSqlQuery

    The SQL works in HeidiSQL and in mysql command.

    I have been searching and I found that QSqlQuery only works with single queries, no way for a list of them

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Complex query in QSqlQuery

    Quote Originally Posted by xgoan View Post
    I have been searching and I found that QSqlQuery only works with single queries, no way for a list of them
    Can't you issue two queries? The @m variable should be tied to the session. And you can always rewrite your queries into a single one.

    Have you considered using sequences?

  10. #10
    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: Complex query in QSqlQuery

    Quote Originally Posted by jacek View Post
    And you can always rewrite your queries into a single one.
    How could I do this in one query?

    Quote Originally Posted by jacek View Post
    Have you considered using sequences?
    I dont' know about sequences

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Complex query in QSqlQuery

    Quote Originally Posted by xgoan View Post
    How could I do this in one query?
    I've overlooked the @m:= part.

    Anyway the idea is to self cross join on the result set and count all rows that should be before the current row.

    I.e. something like:
    SQL Code:
    1. SELECT a.x, count(*) FROM result AS a, result AS b WHERE a.x <= b.x GROUP BY a.x ORDER BY a.x
    To copy to clipboard, switch view to plain text mode 
    of course this isn't quite efficient and will work only if a.x is unique.

    Quote Originally Posted by xgoan View Post
    I dont' know about sequences
    Sequence is an object that generates IDs. Usually it doesn't guarantee that IDs will be consecutive, esp. when you roll back transactions, but usually it doesn't matter. Although AFAIR in MySQL sequences are only in form of auto_increment fields.

    If you are going to insert that data back to the database, you can just simply forget about the id field.

  12. The following user says thank you to jacek for this useful post:

    xgoan (15th March 2008)

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Complex query in QSqlQuery

    You can implement a sequence using triggers. MySQL supports them since 5.0

  14. The following user says thank you to wysota for this useful post:

    xgoan (15th March 2008)

  15. #13
    Join Date
    Oct 2006
    Posts
    42
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Complex query in QSqlQuery

    Quote Originally Posted by xgoan View Post

    I have been searching and I found that QSqlQuery only works with single queries, no way for a list of them
    Check out the new QSqlQuery::nextResult() function in Qt 4.4:
    http://doc.trolltech.com/4.4beta/qsq...tml#nextResult

  16. The following user says thank you to mm78 for this useful post:

    xgoan (17th March 2008)

  17. #14
    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: Complex query in QSqlQuery

    Quote Originally Posted by mm78 View Post
    Check out the new QSqlQuery::nextResult() function in Qt 4.4:
    http://doc.trolltech.com/4.4beta/qsq...tml#nextResult
    This could help only if it supports the SQL vars. Anyway I'm using Qt 4.3.4

Similar Threads

  1. Problems with QSqlQuery update
    By whoops.slo in forum Qt Programming
    Replies: 4
    Last Post: 28th August 2006, 07:17

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.