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
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Complex query in QSqlQuery

    Hi,

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

    And I try to execute it with QSqlQuery and it returns error. I prepare and bind the values for :beginDate and :endDate before executing it.

    Can't be used querys with vars from QSqlQuery?
    Last edited by wysota; 14th March 2008 at 11:16.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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 does QSqlQuery:lastError() together with QSqlError::text() return?

  3. #3
    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. "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';
    2. SELECT
    3. @m:=@m+1 AS 'id',
    4. t.date,
    5. o.name,
    6. t.amount
    7. FROM `tran' at line 8 QMYSQL3: Unable to prepare statement"
    To copy to clipboard, switch view to plain text mode 

    This is the error, I think the problem is the SQL var

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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()?

  5. #5
    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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()?

  7. #7
    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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.

  9. #9
    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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?

  11. #11
    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

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