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 can I set a NULL value with QSqlQuery::addBindValue()?

How can I set a NULL value with QSqlQuery::addBindValue()?

Scheduled Pinned Locked Moved Solved General and Desktop
sql
7 Posts 2 Posters 699 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.
  • J Offline
    J Offline
    jdent
    wrote on 29 Apr 2024, 20:30 last edited by
    #1

    I have a sql string like this in QSqlQuery

    "insert into BankAccountStatement_Scotia(id, fk_CompoundStatement, owner, account, isDollarAccount, initialBalance, booksBalance, retenidoDiferido, availableBalance, date, inputFile) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"

    what parameter can I feed addBindValue to make it represent NULL? Or do I absolutely need to change my sql string by removing the field that I want to leave NULL?

    J 1 Reply Last reply 29 Apr 2024, 21:11
    0
    • J jdent
      29 Apr 2024, 21:50

      @JonB You are correct. Reading the value returns QVariant{} (debugger reads (null))... But how do I check for a null value?

      I tried this:

      		QVariant value = rec.value(1);
      		if (rec.value(1) == QVariant())
      		{
      			fk_CompoundStatement = std::nullopt;
      		}
      

      but the if does not return true even though value is (null) according to debugger!!

      J Offline
      J Offline
      JonB
      wrote on 29 Apr 2024, 21:57 last edited by
      #6

      @jdent bool QVariant::isNull() const

      1 Reply Last reply
      1
      • J jdent
        29 Apr 2024, 20:30

        I have a sql string like this in QSqlQuery

        "insert into BankAccountStatement_Scotia(id, fk_CompoundStatement, owner, account, isDollarAccount, initialBalance, booksBalance, retenidoDiferido, availableBalance, date, inputFile) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"

        what parameter can I feed addBindValue to make it represent NULL? Or do I absolutely need to change my sql string by removing the field that I want to leave NULL?

        J Offline
        J Offline
        JonB
        wrote on 29 Apr 2024, 21:11 last edited by
        #2

        @jdent Did you try QVariant()?

        J 1 Reply Last reply 29 Apr 2024, 21:28
        2
        • J JonB
          29 Apr 2024, 21:11

          @jdent Did you try QVariant()?

          J Offline
          J Offline
          jdent
          wrote on 29 Apr 2024, 21:28 last edited by
          #3

          @JonB It actually works!!
          However when reading the record instead of NULL it is represented by 0 (the field is an optional integer -- that is std::optional<int>).

          So if I compare the record going in with the record read they are not the same!!

          Any way around this?

          Intercepting the reading from the database to represent the NULL value as a std::nullopt??

          J 1 Reply Last reply 29 Apr 2024, 21:38
          0
          • J jdent
            29 Apr 2024, 21:28

            @JonB It actually works!!
            However when reading the record instead of NULL it is represented by 0 (the field is an optional integer -- that is std::optional<int>).

            So if I compare the record going in with the record read they are not the same!!

            Any way around this?

            Intercepting the reading from the database to represent the NULL value as a std::nullopt??

            J Offline
            J Offline
            JonB
            wrote on 29 Apr 2024, 21:38 last edited by
            #4

            @jdent
            I don't know, but show how you are seeing a 0? I would have hoped it would come back as QVariant() too? Of course make sure that the table actually holds a NULL and not a 0 after your INSERT.

            J 1 Reply Last reply 29 Apr 2024, 21:50
            1
            • J JonB
              29 Apr 2024, 21:38

              @jdent
              I don't know, but show how you are seeing a 0? I would have hoped it would come back as QVariant() too? Of course make sure that the table actually holds a NULL and not a 0 after your INSERT.

              J Offline
              J Offline
              jdent
              wrote on 29 Apr 2024, 21:50 last edited by
              #5

              @JonB You are correct. Reading the value returns QVariant{} (debugger reads (null))... But how do I check for a null value?

              I tried this:

              		QVariant value = rec.value(1);
              		if (rec.value(1) == QVariant())
              		{
              			fk_CompoundStatement = std::nullopt;
              		}
              

              but the if does not return true even though value is (null) according to debugger!!

              J J 2 Replies Last reply 29 Apr 2024, 21:57
              0
              • J jdent
                29 Apr 2024, 21:50

                @JonB You are correct. Reading the value returns QVariant{} (debugger reads (null))... But how do I check for a null value?

                I tried this:

                		QVariant value = rec.value(1);
                		if (rec.value(1) == QVariant())
                		{
                			fk_CompoundStatement = std::nullopt;
                		}
                

                but the if does not return true even though value is (null) according to debugger!!

                J Offline
                J Offline
                JonB
                wrote on 29 Apr 2024, 21:57 last edited by
                #6

                @jdent bool QVariant::isNull() const

                1 Reply Last reply
                1
                • J jdent
                  29 Apr 2024, 21:50

                  @JonB You are correct. Reading the value returns QVariant{} (debugger reads (null))... But how do I check for a null value?

                  I tried this:

                  		QVariant value = rec.value(1);
                  		if (rec.value(1) == QVariant())
                  		{
                  			fk_CompoundStatement = std::nullopt;
                  		}
                  

                  but the if does not return true even though value is (null) according to debugger!!

                  J Offline
                  J Offline
                  jdent
                  wrote on 29 Apr 2024, 21:58 last edited by
                  #7

                  @jdent I know: value.isNull()!!

                  1 Reply Last reply
                  0
                  • J jdent has marked this topic as solved on 29 Apr 2024, 21:58

                  7/7

                  29 Apr 2024, 21:58

                  • Login

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