Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to fetch Data Between Two Dates ?
Forum Updated to NodeBB v4.3 + New Features

How to fetch Data Between Two Dates ?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ramkumar Mohan
    wrote on 16 Aug 2022, 07:24 last edited by
    #1

    I wrote this code on how to fetch data between two dates ( FROM date to To date).?

    QDate fdate= ui->dateEdit_4->date();
    QString fdateString = fdate.toString("dd / MM / yyyy");
    QDate tdate= ui->dateEdit_5->date();
    QString tdateString = tdate.toString("dd / MM / yyyy");
    QSqlQueryModel * modal = new QSqlQueryModel();
    QSqlQuery* query=new QSqlQuery(mydb);
    query->prepare("SELECT PID , TName , ANrmlRang , BNrmlRang , ODVal , ResultOD , Date , Time FROM Reports WHERE Date BETWEEN '"+fdateString+"' AND '"+tdateString+"'");

    J J 2 Replies Last reply 16 Aug 2022, 07:45
    0
    • R Ramkumar Mohan
      16 Aug 2022, 07:24

      I wrote this code on how to fetch data between two dates ( FROM date to To date).?

      QDate fdate= ui->dateEdit_4->date();
      QString fdateString = fdate.toString("dd / MM / yyyy");
      QDate tdate= ui->dateEdit_5->date();
      QString tdateString = tdate.toString("dd / MM / yyyy");
      QSqlQueryModel * modal = new QSqlQueryModel();
      QSqlQuery* query=new QSqlQuery(mydb);
      query->prepare("SELECT PID , TName , ANrmlRang , BNrmlRang , ODVal , ResultOD , Date , Time FROM Reports WHERE Date BETWEEN '"+fdateString+"' AND '"+tdateString+"'");

      J Offline
      J Offline
      JonB
      wrote on 16 Aug 2022, 07:45 last edited by JonB
      #2

      @Ramkumar-Mohan
      And? Please actually ask a question! What happened? Did you get an error? Did it not behave as you expected? In what way, what did you see or not see?

      What do you actually expect to happen from this code as shown...?

      R 1 Reply Last reply 16 Aug 2022, 10:16
      0
      • A Offline
        A Offline
        ali-aydin
        wrote on 16 Aug 2022, 08:13 last edited by
        #3

        Hi,
        You must have date field in your data and when you are querying you can
        fetch data by "where" expression if you are using SQL

        J 1 Reply Last reply 16 Aug 2022, 09:00
        0
        • A ali-aydin
          16 Aug 2022, 08:13

          Hi,
          You must have date field in your data and when you are querying you can
          fetch data by "where" expression if you are using SQL

          J Offline
          J Offline
          JonB
          wrote on 16 Aug 2022, 09:00 last edited by
          #4

          @ali-aydin
          The OP shows a Date field, and uses it in a WHERE clause already. I don't think that in itself is any issue.

          1 Reply Last reply
          0
          • R Ramkumar Mohan
            16 Aug 2022, 07:24

            I wrote this code on how to fetch data between two dates ( FROM date to To date).?

            QDate fdate= ui->dateEdit_4->date();
            QString fdateString = fdate.toString("dd / MM / yyyy");
            QDate tdate= ui->dateEdit_5->date();
            QString tdateString = tdate.toString("dd / MM / yyyy");
            QSqlQueryModel * modal = new QSqlQueryModel();
            QSqlQuery* query=new QSqlQuery(mydb);
            query->prepare("SELECT PID , TName , ANrmlRang , BNrmlRang , ODVal , ResultOD , Date , Time FROM Reports WHERE Date BETWEEN '"+fdateString+"' AND '"+tdateString+"'");

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 16 Aug 2022, 09:41 last edited by
            #5

            @Ramkumar-Mohan
            couple of thing to note, first,

            you're supposed to bindValue() to bind to placeholders/values in your query. Construction the query in place is error prone and unreadable.

            • AFAIK the query is supposed to end on a ;not sure if it's mandatory or not.
            • dd / MM / yyyy is not the default format for dates in SQL, that would be YYYY-MM-DD are you sure your format is correct, and not simply 2 strings that are compared ?
            • if the type of your db is actually DATE , than use QDate that one is actually mapped to DATE. https://doc.qt.io/qt-6/sql-types.html
            • when you execute the query and it returns false, what does https://doc.qt.io/qt-6/qsqlquery.html#lastError actually tell you ?

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            J 1 Reply Last reply 16 Aug 2022, 10:01
            2
            • J J.Hilk
              16 Aug 2022, 09:41

              @Ramkumar-Mohan
              couple of thing to note, first,

              you're supposed to bindValue() to bind to placeholders/values in your query. Construction the query in place is error prone and unreadable.

              • AFAIK the query is supposed to end on a ;not sure if it's mandatory or not.
              • dd / MM / yyyy is not the default format for dates in SQL, that would be YYYY-MM-DD are you sure your format is correct, and not simply 2 strings that are compared ?
              • if the type of your db is actually DATE , than use QDate that one is actually mapped to DATE. https://doc.qt.io/qt-6/sql-types.html
              • when you execute the query and it returns false, what does https://doc.qt.io/qt-6/qsqlquery.html#lastError actually tell you ?
              J Offline
              J Offline
              JonB
              wrote on 16 Aug 2022, 10:01 last edited by
              #6

              @J-Hilk said in How to fetch Data Between Two Dates ?:

              when you execute the query and it returns false

              But that was my point: what does the user expect, it shows a prepare() and no exec() so nothing is going to happen....

              1 Reply Last reply
              0
              • J JonB
                16 Aug 2022, 07:45

                @Ramkumar-Mohan
                And? Please actually ask a question! What happened? Did you get an error? Did it not behave as you expected? In what way, what did you see or not see?

                What do you actually expect to happen from this code as shown...?

                R Offline
                R Offline
                Ramkumar Mohan
                wrote on 16 Aug 2022, 10:16 last edited by Ramkumar Mohan
                #7

                @JonB

                d.JPG

                As shown in this photo, if I check (From date to To date), I want to find the data within those days. I wrote the above code and it didn't work for me.

                J 1 Reply Last reply 16 Aug 2022, 10:20
                0
                • R Ramkumar Mohan
                  16 Aug 2022, 10:16

                  @JonB

                  d.JPG

                  As shown in this photo, if I check (From date to To date), I want to find the data within those days. I wrote the above code and it didn't work for me.

                  J Offline
                  J Offline
                  JonB
                  wrote on 16 Aug 2022, 10:20 last edited by JonB
                  #8

                  @Ramkumar-Mohan

                  I wrote the above code and it didn't work for me.

                  The above code does not execute any SQL query. So in itself it won't do anything, like I said. I don't know whether you have other code to execute it, because you don't say; if you do, why not say so, we cannot guess?

                  For the rest start with @J-Hilk's comments.

                  R 1 Reply Last reply 16 Aug 2022, 10:32
                  0
                  • J JonB
                    16 Aug 2022, 10:20

                    @Ramkumar-Mohan

                    I wrote the above code and it didn't work for me.

                    The above code does not execute any SQL query. So in itself it won't do anything, like I said. I don't know whether you have other code to execute it, because you don't say; if you do, why not say so, we cannot guess?

                    For the rest start with @J-Hilk's comments.

                    R Offline
                    R Offline
                    Ramkumar Mohan
                    wrote on 16 Aug 2022, 10:32 last edited by
                    #9

                    @JonB

                    Sorry sir, I shared my code, please find the Below Attachment

                    da.JPG

                    J 1 Reply Last reply 16 Aug 2022, 10:39
                    0
                    • R Ramkumar Mohan
                      16 Aug 2022, 10:32

                      @JonB

                      Sorry sir, I shared my code, please find the Below Attachment

                      da.JPG

                      J Offline
                      J Offline
                      JonB
                      wrote on 16 Aug 2022, 10:39 last edited by JonB
                      #10

                      @Ramkumar-Mohan
                      OK, at least now I know. You might be surprised --- we have indeed had to people who prepare queries but never execute them!

                      You don't check the return result from the exec(). You don't tell me whether it returns any rows. If I am to guess, are you saying "it does return rows, but it returns too many rows, it returns rows outside of my desired date range"? That is what I need to hear!

                      So like I said start with @J-Hilk's observations. Above all, how do we know whether

                      BETWEEN '03 / 03 / 2022' AND  '05 / 06 / 2022'
                      

                      is even acceptable to your SQL implementation?

                      Print out exactly what the query string is. Try it in whatever command-line tool you have for sending queries to your database. Try it with a plain Date = ... instead of BETWEEN to test the format if you wonder whether BETWEEN works. And/or use a bound variable to pass a QDate value instead of trying to construct a literal string for it.

                      R 2 Replies Last reply 16 Aug 2022, 10:47
                      2
                      • J JonB
                        16 Aug 2022, 10:39

                        @Ramkumar-Mohan
                        OK, at least now I know. You might be surprised --- we have indeed had to people who prepare queries but never execute them!

                        You don't check the return result from the exec(). You don't tell me whether it returns any rows. If I am to guess, are you saying "it does return rows, but it returns too many rows, it returns rows outside of my desired date range"? That is what I need to hear!

                        So like I said start with @J-Hilk's observations. Above all, how do we know whether

                        BETWEEN '03 / 03 / 2022' AND  '05 / 06 / 2022'
                        

                        is even acceptable to your SQL implementation?

                        Print out exactly what the query string is. Try it in whatever command-line tool you have for sending queries to your database. Try it with a plain Date = ... instead of BETWEEN to test the format if you wonder whether BETWEEN works. And/or use a bound variable to pass a QDate value instead of trying to construct a literal string for it.

                        R Offline
                        R Offline
                        Ramkumar Mohan
                        wrote on 16 Aug 2022, 10:47 last edited by
                        #11

                        @JonB Ok

                        1 Reply Last reply
                        0
                        • J JonB
                          16 Aug 2022, 10:39

                          @Ramkumar-Mohan
                          OK, at least now I know. You might be surprised --- we have indeed had to people who prepare queries but never execute them!

                          You don't check the return result from the exec(). You don't tell me whether it returns any rows. If I am to guess, are you saying "it does return rows, but it returns too many rows, it returns rows outside of my desired date range"? That is what I need to hear!

                          So like I said start with @J-Hilk's observations. Above all, how do we know whether

                          BETWEEN '03 / 03 / 2022' AND  '05 / 06 / 2022'
                          

                          is even acceptable to your SQL implementation?

                          Print out exactly what the query string is. Try it in whatever command-line tool you have for sending queries to your database. Try it with a plain Date = ... instead of BETWEEN to test the format if you wonder whether BETWEEN works. And/or use a bound variable to pass a QDate value instead of trying to construct a literal string for it.

                          R Offline
                          R Offline
                          Ramkumar Mohan
                          wrote on 16 Aug 2022, 12:06 last edited by
                          #12

                          @JonB

                          1.JPG

                          Now , this code is working fine sir, Thank you.

                          1 Reply Last reply
                          0

                          1/12

                          16 Aug 2022, 07:24

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved