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
Forum Updated to NodeBB v4.3 + New Features

MariaDB problem inserting NULL date

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 1.2k 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 14 Nov 2020, 19:34 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Nov 2020, 23:08 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 15 Nov 2020, 14:57 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?

        C 1 Reply Last reply 15 Nov 2020, 15:59
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 Nov 2020, 15:03 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 15 Nov 2020, 15:58 last edited by
            #5

            Ah ok!
            Without data_dimissione works fine.

            1 Reply Last reply
            0
            • S Stefanoxjx
              15 Nov 2020, 14:57

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

              C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 15 Nov 2020, 15:59 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 15 Nov 2020, 16:05
              1
              • C Christian Ehrlicher
                15 Nov 2020, 15:59

                @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 15 Nov 2020, 16:05 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
                • C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 15 Nov 2020, 16:24 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

                  J 1 Reply Last reply 15 Nov 2020, 18:24
                  2
                  • C Christian Ehrlicher
                    15 Nov 2020, 16:24

                    @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?

                    J Offline
                    J Offline
                    JonB
                    wrote on 15 Nov 2020, 18:24 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
                    • C Online
                      C Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 15 Nov 2020, 18:46 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 15 Nov 2020, 19:22 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.

                        C 1 Reply Last reply 15 Nov 2020, 19:49
                        3
                        • S Stefanoxjx
                          15 Nov 2020, 19:22

                          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.

                          C Online
                          C Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 15 Nov 2020, 19:49 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

                          1/12

                          14 Nov 2020, 19:34

                          • 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