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. Problem with MariaDB Query

Problem with MariaDB Query

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 246 Views
  • 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.
  • S Offline
    S Offline
    Stefanoxjx
    wrote on last edited by Stefanoxjx
    #1

    Hi guys,
    I've a problem with a query used with MariaDB.
    I state that all other queries in the program works fine.
    This is the code tha generate and execute the query:

       QSqlQuery Query;
    
       Query.prepare("SELECT d.*, dv.*, me.Descrizione FROM dipendenti d "
                      "LEFT JOIN (SELECT * FROM ("
                      "   SELECT *, row_number() over(partition by Dipendenti_Id order BY DataVisita desc) as rn "
                      "   FROM visitemediche) dv "
                      "   WHERE dv.rn = 1) dv ON d.id = dv.dipendenti_id "
                      "LEFT JOIN tipiscadenze t ON dv.TipiScadenze_id = t.Id "
                      "LEFT JOIN (SELECT * FROM ("
                      "  SELECT *, row_number() over(partition by Dipendenti_Id order by Dataformazione desc) as rn "
                      "  FROM mansioni) m "
                      "  WHERE m.rn = 1) m ON d.id = m.dipendenti_id "
                      "LEFT JOIN mansioni_elenco me ON m.Mansioni_Elenco_Id = me.id "
                      "LEFT JOIN (SELECT * FROM ("
                      "   SELECT *, row_number() over(partition by Dipendenti_Id order by DataDimissione DESC) as rn "
                      "   FROM assunzioni) a "
                      "   WHERE a.rn = 1) a ON a.dipendenti_id = d.Id "
                      "WHERE Cooperative_Id = :Cooperative_Id "
                      "AND dv.dataVisita IS NOT NULL "
                      "AND dv.tipovisita = :TipoVisita "
                      "AND a.dataassunzione IS NOT NULL AND datadimissione IS NULL "
                      "AND DATE_ADD(dv.DataVisita, INTERVAL t.Scadenza DAY) < :DataScadenza "
                      "GROUP BY d.Id");
    
    
        Query.bindValue(":Cooperative_Id", QString::number(ForYou::GetAziendaId()));
        Query.bindValue(":TipoVisita", "'"+cmbTipoVisita->currentData().toString()+"'");
    
        QDate DataScad = QDate::fromString(de->text(), "dd/MM/yyyy");
        Query.bindValue(":DataScadenza", "'"+DataScad.toString("yyyyMMdd")+"'");
    
        Query.exec();
    

    The generated query is this:

    "SELECT d.*, dv.*, me.Descrizione FROM dipendenti d LEFT JOIN (SELECT * FROM (   SELECT *, row_number() over(partition by Dipendenti_Id order BY DataVisita desc) as rn    FROM visitemediche) dv    WHERE dv.rn = 1) dv ON d.id = dv.dipendenti_id LEFT JOIN tipiscadenze t ON dv.TipiScadenze_id = t.Id LEFT JOIN (SELECT * FROM (  SELECT *, row_number() over(partition by Dipendenti_Id order by Dataformazione desc) as rn   FROM mansioni) m   WHERE m.rn = 1) m ON d.id = m.dipendenti_id LEFT JOIN mansioni_elenco me ON m.Mansioni_Elenco_Id = me.id LEFT JOIN (SELECT * FROM (   SELECT *, row_number() over(partition by Dipendenti_Id order by DataDimissione DESC) as rn    FROM assunzioni) a    WHERE a.rn = 1) a ON a.dipendenti_id = d.Id WHERE Cooperative_Id = 1 AND dv.dataVisita IS NOT NULL AND dv.tipovisita = 'O' AND a.dataassunzione IS NOT NULL AND datadimissione IS NULL AND DATE_ADD(dv.DataVisita, INTERVAL t.Scadenza DAY) < '20210703' GROUP BY d.Id"
    

    and if I try it in HeidiSQL it works fine, but executed from Qt the result of Query.size() is always == 0.
    I don't understand the problem.
    Maybe can be a too long query?

    Thanks.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Did you check whether you have an error ?
      Did you print it ?

      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
      1
      • S Offline
        S Offline
        Stefanoxjx
        wrote on last edited by
        #3

        Hi SGaist,
        yes, I do it.

        qDebug() << "Errors: " << Query.lastError() << Query.lastError().nativeErrorCode();
        

        and the result is:

        Errors:  QSqlError("", "", "") ""
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I just thought, not all database system returns the query size.

          Did you try to go through the result of the query ?

          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
          2
          • M Offline
            M Offline
            mmiacca
            wrote on last edited by
            #5

            @Stefanoxjx said in Problem with MariaDB Query:

            "'"
            Hi

            You must remove "'" from de parameters of bindValue, the "'" are added by bindValue depending on data type, e.g. added in string, not added in integers.

            Sorry for my english

            S 1 Reply Last reply
            4
            • M mmiacca

              @Stefanoxjx said in Problem with MariaDB Query:

              "'"
              Hi

              You must remove "'" from de parameters of bindValue, the "'" are added by bindValue depending on data type, e.g. added in string, not added in integers.

              Sorry for my english

              S Offline
              S Offline
              Stefanoxjx
              wrote on last edited by
              #6

              Hi mmiacca and thanks for your help.
              You're right, the problem was generated from this line:

              Query.bindValue(":DataScadenza", "'"+DataScad.toString("yyyyMMdd")+"'");
              

              I changed it in:

              Query.bindValue(":DataScadenza", DataScad.toString("yyyyMMdd"));
              

              and the query works fine.

              Don't worry for your english, me too is not very good in english :)

              Regards.

              1 Reply Last reply
              0

              • Login

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