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

    JonBJ 1 Reply Last reply
    0
    • J jdent

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

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

      @jdent bool QVariant::isNull() const

      1 Reply Last reply
      1
      • J jdent

        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?

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

        @jdent Did you try QVariant()?

        J 1 Reply Last reply
        2
        • JonBJ JonB

          @jdent Did you try QVariant()?

          J Offline
          J Offline
          jdent
          wrote on 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??

          JonBJ 1 Reply Last reply
          0
          • J jdent

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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
            1
            • JonBJ JonB

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

              JonBJ J 2 Replies Last reply
              0
              • J jdent

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

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

                @jdent bool QVariant::isNull() const

                1 Reply Last reply
                1
                • J jdent

                  @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 last edited by
                  #7

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

                  1 Reply Last reply
                  0
                  • J jdent has marked this topic as solved on

                  • Login

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