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. MariaDB problem inserting NULL date

MariaDB problem inserting NULL date

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 1.5k Views 2 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.
  • S Offline
    S Offline
    Stefanoxjx
    wrote on last edited by
    #1

    Hi, I've a problem inserting NULL date in a table.
    This is the query generated from Query.prepare and Query.BindValue:

    INSERT INTO Assunzioni (Dipendenti_id, Livelli_id, Scadenze_id, data_assunzione, data_dimissione) VALUES(2, 1, 1, 20201114, NULL)
    

    it give me error:

    QSqlError("1292", "QMYSQL3: Unable to execute statement", "Incorrect date value: 'NULL' for column `db`.`assunzioni`.`data_dimissione` at row 1")
    

    But same query, executed in HeidiSQL, works fine.
    If in Qt I replace NULL with a date I haven't problems.

    This is the definition of table:

    CREATE TABLE `Assunzioni` (
      `id` INT NOT NULL AUTO_INCREMENT,
      `Dipendenti_id` INT NOT NULL,
      `Livelli_id` INT NOT NULL,
      `Scadenze_id` INT NOT NULL,
      `data_assunzione` DATE NOT NULL,
      `data_dimissione` DATE NULL,
      PRIMARY KEY (`id`),
      KEY(`Dipendenti_id`))
    ENGINE = InnoDB;
    

    In many case, I need to leave empty data_dimissione.
    How I can do it?

    Thanks.

    Stefano

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

      Hi,

      Did you try to just not mention that filed at all in your insert 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
      1
      • S Offline
        S Offline
        Stefanoxjx
        wrote on last edited by Stefanoxjx
        #3

        Hi SGaist, if I comment this line: Query.bindValue(":data_dimissione", Dimissione); I haven't errors.
        I this the correct way?

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

          I would have tested with the equivalent of:

          INSERT INTO Assunzioni (Dipendenti_id, Livelli_id, Scadenze_id, data_assunzione) VALUES(2, 1, 1, 20201114)

          So no mention of data_dimissione in the query at all.

          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
            #5

            Ah ok!
            Without data_dimissione works fine.

            1 Reply Last reply
            0
            • S Stefanoxjx

              Hi SGaist, if I comment this line: Query.bindValue(":data_dimissione", Dimissione); I haven't errors.
              I this the correct way?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Stefanoxjx said in MariaDB problem inserting NULL date:

              Query.bindValue(":data_dimissione", Dimissione);

              What's the value of Dimissione in this case?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              S 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @Stefanoxjx said in MariaDB problem inserting NULL date:

                Query.bindValue(":data_dimissione", Dimissione);

                What's the value of Dimissione in this case?

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

                @Christian-Ehrlicher said in MariaDB problem inserting NULL date:

                @Stefanoxjx said in MariaDB problem inserting NULL date:

                Query.bindValue(":data_dimissione", Dimissione);

                What's the value of Dimissione in this case?

                If Dimissione is (for example) 20201115 all works fine.
                If Dimissione is NULL I've an error.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Stefanoxjx said in MariaDB problem inserting NULL date:

                  If Dimissione is NULL I've an error.

                  Since it's a QVariant, it can't be 'NULL' - only QVariant::isNull() or isValid() can return true. Do you mean than QVariant::isNull() returned true?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  JonBJ 1 Reply Last reply
                  2
                  • Christian EhrlicherC Christian Ehrlicher

                    @Stefanoxjx said in MariaDB problem inserting NULL date:

                    If Dimissione is NULL I've an error.

                    Since it's a QVariant, it can't be 'NULL' - only QVariant::isNull() or isValid() can return true. Do you mean than QVariant::isNull() returned true?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Christian-Ehrlicher
                    Doubtless you are right, and this will prove the nub of the issue, but the OP said originally:

                    INSERT INTO Assunzioni (Dipendenti_id, Livelli_id, Scadenze_id, data_assunzione, data_dimissione) VALUES(2, 1, 1, 20201114, NULL)

                    and

                    Incorrect date value: 'NULL' for column db.assunzioni.data_dimissione at row 1

                    but you'd think that NULL at the end implied that the statement had been generated as desired?

                    1 Reply Last reply
                    1
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      I'm not sure if the prepared statement quotes strings so it can still be NULL as string, therefore my question. And if it's really QVariant::isNull() then a simple reproducable example would be nice.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      3
                      • S Offline
                        S Offline
                        Stefanoxjx
                        wrote on last edited by
                        #11

                        Reading your reply, I tought that my error was to have defined Dimissione as QString.
                        Then, I've tried this solution:

                        if(...condition...) QVariant Dimissione = QVariant();
                        qDebug() << "QVariant::IsNull " << Dimissione.isNull();
                        ...
                        Query.bindValue(":data_dimissione", Dimissione);
                        ...
                        

                        So, QVarinat::IsNull return true, and I haven't errors in Query.exec()

                        I learned a new thing :)

                        Thanks for your invaluable help.

                        Christian EhrlicherC 1 Reply Last reply
                        3
                        • S Stefanoxjx

                          Reading your reply, I tought that my error was to have defined Dimissione as QString.
                          Then, I've tried this solution:

                          if(...condition...) QVariant Dimissione = QVariant();
                          qDebug() << "QVariant::IsNull " << Dimissione.isNull();
                          ...
                          Query.bindValue(":data_dimissione", Dimissione);
                          ...
                          

                          So, QVarinat::IsNull return true, and I haven't errors in Query.exec()

                          I learned a new thing :)

                          Thanks for your invaluable help.

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Stefanoxjx Nice to hear, can you please mark this topic as solved then?

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          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