Results 1 to 7 of 7

Thread: cannot get max(column) value in sql.

  1. #1
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default cannot get max(column) value in sql.

    hi to all, I've Centos7.5 as client and centos6.10 as mysql server. i am building small project and here i want to get maximum value from billno ( bill number ). I've tried this code :-
    Qt Code:
    1. int Sales::setmaxno()
    2. {
    3. QSqlQuery qury;
    4. QString sql= "select max(billno) from tableSale";
    5.  
    6. query->prepare(sql);
    7. if(query->exec() )
    8. {
    9. if(qury.next())
    10. {
    11. Sales::maxno = query->value(0).toInt();
    12. return Sales::maxno;
    13. }
    14. }
    15. else
    16. {
    17. QMessageBox::information(this, "Sales", "in else " + query->lastError().text() );
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    but this shows maxno = 0; always.
    i think query "select max(billno) from tableSale"; is not selecting max of column.
    how to get solved this problem.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot get max(column) value in sql.

    First of all : this code should not compile.

  3. #3
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: cannot get max(column) value in sql.

    you are right this code is not compiling but Why? please explain.
    Last edited by rahulvishwakarma; 1st May 2020 at 16:05.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot get max(column) value in sql.

    Because the return... line is missing before line 19. The compiler certainly gave the appropriate message.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: cannot get max(column) value in sql.

    Sales::maxno
    Is "maxno" a static member variable of the Sales class? Why do you qualify it with the "Sales::" class scope? Look at your C++ textbook if you do not understand this.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: cannot get max(column) value in sql.

    yes maxno is static member

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: cannot get max(column) value in sql.

    Qt Code:
    1. int Sales::setmaxno()
    2. {
    3. QSqlQuery qury;
    4. QString sql= "select max(billno) from tableSale";
    5.  
    6. query->prepare(sql);
    7. if(query->exec() )
    8. {
    9. if(qury.next())
    10. {
    11. Sales::maxno = query->value(0).toInt();
    12. return Sales::maxno;
    13. }
    14. }
    15. else
    16. {
    17. QMessageBox::information(this, "Sales", "in else " + query->lastError().text() );
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    The variable "qury" at line 3 and 9 is not related to the undeclared (member?) variable "query" at lines 6, 7, and 11. You prepare() and exec() one QSqlQuery object then attempt to navigate another (invalid) object. If that worked, you would return value from the first QSqlQuery object, which is pointing at an invalid record. Since "qury" is not active the next() call will fail and your code (if it compiles at all) will return an undefined value because there is no other return statement.

Similar Threads

  1. Replies: 1
    Last Post: 9th December 2015, 19:23
  2. Replies: 1
    Last Post: 20th November 2015, 10:12
  3. Replies: 4
    Last Post: 27th February 2014, 10:42
  4. Replies: 3
    Last Post: 9th October 2012, 03:12
  5. Replies: 3
    Last Post: 5th May 2011, 15:03

Tags for this Thread

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.