I am using QtSql module with a MySQL back end. I prefer to using bindValue() as much as is possible for input values in queries.
I have a case where I need to SELECT and look for a NULL in the WHERE clause.
A simplified example of what I need to do [effectively]:
SELECT * FROM rabbits WHERE color IS NULL;
All the binding examples in the documentation show only INSERT statements, however. The documentation says to bind the QVariant(QVariant::String) to the ":color" term.
{
// stuff
query.prepare("SELECT * FROM rabbits WHERE color=:color");
if (color.isEmpty())
else
query.bindValue(":color", color);
query.exec();
// more stuff
}
void do_query(QString color)
{
// stuff
query.prepare("SELECT * FROM rabbits WHERE color=:color");
if (color.isEmpty())
query.bindValue(":color", QVariant(QVariant::String));
else
query.bindValue(":color", color);
query.exec();
// more stuff
}
To copy to clipboard, switch view to plain text mode
Some of the documentation wording gives me pause and I think it is probably specific to an INSERT statement which would convert to "=NULL" when making the MySQL call underneath. A where statement would need to be converted to " IS NULL" instead.
Is there is a way use binding to SELECT items matching a NULL as well as those matching a value?
Bookmarks