Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. QSqlQuery Sqlite3 specific SELECT for datetime
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery Sqlite3 specific SELECT for datetime

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
6 Posts 2 Posters 3.3k 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.
  • M Offline
    M Offline
    MrLibya
    wrote on 9 Nov 2016, 19:22 last edited by MrLibya 11 Oct 2016, 10:47
    #1

    Hello
    I"m trying to make a specific SELECT for the QSqlQueryModel and show on the QTableView , so the problem is nothing show on the
    In the first to check that the full code is ok i test

    CartItems->setQuery(QString("SELECT * from main.sell_cash_log "));
    

    and it's work , it show all items , now when i use what i want :

    CartItems->setQuery(QString("SELECT * from main.sell_cash_log WHERE date('when') BETWEEN date('%1') AND date('%2')").arg(ui->fromdate->dateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(ui->todate->dateTime().toString("yyyy-MM-dd hh:mm:ss")));
    
    

    and i tried

    CartItems->setQuery(QString("SELECT * from main.sell_cash_log WHERE strftime('%Y-%m-%d %H:%M:%S','when') BETWEEN '%1' AND '%2'").arg(ui->fromdate->dateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(ui->todate->dateTime().toString("yyyy-MM-dd hh:mm:ss")));
    
    

    and didn't work ! ,
    i used

    qDebug()<<CartItems->query().lastQuery(); // to get the query
    "SELECT * from main.sell_cash_log WHERE date('when') BETWEEN date('2015-11-09 00:00:00') AND date('2016-11-09 00:00:00')"
    

    and that is my table :
    alt text

    And the two query didn't work !

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Nov 2016, 21:54 last edited by
      #2

      Hi,

      See this stack overflow post and the following answer.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 10 Nov 2016, 10:46
      1
      • S SGaist
        9 Nov 2016, 21:54

        Hi,

        See this stack overflow post and the following answer.

        Hope it helps

        M Offline
        M Offline
        MrLibya
        wrote on 10 Nov 2016, 10:46 last edited by MrLibya 11 Oct 2016, 10:51
        #3

        @SGaist I test them all but same result , and I've edit my query , now it's makeing a sence but still not work

        CartItems->setQuery(QString("SELECT * from main.sell_cash_log WHERE strftime('%Y-%m-%d %H:%M:%S','when') BETWEEN strftime('%Y-%m-%d %H:%M:%S','%1') AND strftime('%Y-%m-%d %H:%M:%S','%2')").arg(ui->fromdate->dateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(ui->todate->dateTime().toString("yyyy-MM-dd hh:mm:ss")));
        

        there is solution it might work , in the link u give me someone says he make it as intger YYYYMMDD here mydate >= '20090101' and mydate <= '20050505' , but it's not real solution

        and I've think in something like select all the 'when' rows and then make compare each one a head , but with large database it will take alot of time !

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 10 Nov 2016, 21:10 last edited by
          #4

          Shouldn't you use the <= and >= operators with SQLite ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MrLibya
            wrote on 10 Nov 2016, 21:26 last edited by MrLibya 11 Oct 2016, 21:28
            #5

            The problem was by the field called when , in insert query i was useing escape string ('when') but with select not work (' ' ) so i used ( when) and it's workd :
            :

            CartItems->setQuery(QString("SELECT * from main.sell_cash_log WHERE datetime(`when`) BETWEEN datetime('%1') AND datetime('%2')").arg(ui->fromdate->dateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(ui->todate->dateTime().toString("yyyy-MM-dd hh:mm:ss")));
            
            1 Reply Last reply
            1
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 10 Nov 2016, 21:32 last edited by
              #6

              Great !

              Thanks for sharing :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              2/6

              9 Nov 2016, 21:54

              topic:navigator.unread, 4
              • Login

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