Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Complex query in QSqlQuery

  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

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

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

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

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

    xgoan (15th March 2008)

  16. #15
    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 implement a sequence using triggers. MySQL supports them since 5.0

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

    xgoan (15th March 2008)

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

    Thanks
    I will try this query but I think that will be better use 2 QSqlQuery, one for take the count of registers and the other to get the selected registers with C++ code because it seems that SQL vars doesn't work with QSqlQuery .

    Anyway it's a pity that QSqlQuery doesn't support queries like these

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

    Just a side note - I'd use a different query (it may be invalid but I mean the general idea):

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

    MAX is safer than COUNT.

    But honestly that's a perfect usecase for triggers - you can have a trigger that will calculate the id itself and modify the query on the fly.

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

    xgoan (17th March 2008)

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

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

    xgoan (17th March 2008)

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

  24. #20
    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
    Just a side note - I'd use a different query (it may be invalid but I mean the general idea):

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

    MAX is safer than COUNT.

    But honestly that's a perfect usecase for triggers - you can have a trigger that will calculate the id itself and modify the query on the fly.
    It's not a valid query:
    Qt Code:
    1. mysql> DESCRIBE `transactions`;
    2. +-----------+--------------+------+-----+---------+-------+
    3. | Field | Type | Null | Key | Default | Extra |
    4. +-----------+--------------+------+-----+---------+-------+
    5. | cardid | char(16) | NO | PRI | | |
    6. | date | datetime | NO | PRI | | |
    7. | operation | int(11) | NO | | | |
    8. | amount | decimal(4,2) | NO | | 0.00 | |
    9. +-----------+--------------+------+-----+---------+-------+
    10. 4 rows in set (0.00 sec)
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. mysql> [B]SELECT * FROM transactions LIMIT 1;[/B]
    2. +------------------+---------------------+-----------+--------+
    3. | cardid | date | operation | amount |
    4. +------------------+---------------------+-----------+--------+
    5. | %07032008125843_ | 2008-03-11 14:50:32 | 1000 | 5.00 |
    6. +------------------+---------------------+-----------+--------+
    7. 1 row in set (0.00 sec)
    To copy to clipboard, switch view to plain text mode 

    As you can see I can't use MAX(cardid) because it's not correlative number. And the primary key it's done with cardid+date

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.