How can I set a NULL value with QSqlQuery::addBindValue()?
-
wrote on 29 Apr 2024, 20:30 last edited by
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?
-
@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!!
wrote on 29 Apr 2024, 21:57 last edited by -
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?
-
wrote on 29 Apr 2024, 21:28 last edited by
@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??
-
@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??
-
@jdent
I don't know, but show how you are seeing a0
? I would have hoped it would come back asQVariant()
too? Of course make sure that the table actually holds aNULL
and not a0
after yourINSERT
.wrote on 29 Apr 2024, 21:50 last edited by@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!!
-
@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!!
wrote on 29 Apr 2024, 21:57 last edited by -
@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!!
-
7/7