The simplest way would just be:
bool ok = false;
if(color.isEmpty()){
ok = query.prepare("SELECT * FROM rabbits WHERE color IS NULL");
} else {
ok = query.prepare("SELECT * FROM rabbits WHERE color=:color");
query.bindValue(":color", color);
}
if(ok)
query.exec();
bool ok = false;
if(color.isEmpty()){
ok = query.prepare("SELECT * FROM rabbits WHERE color IS NULL");
} else {
ok = query.prepare("SELECT * FROM rabbits WHERE color=:color");
query.bindValue(":color", color);
}
if(ok)
query.exec();
To copy to clipboard, switch view to plain text mode
or:
query.
prepare(QString("SELECT * FROM rabbits WHERE color") + (color.
isEmpty() ?
" IS NULL" : "=:color"));
if(!color.isEmpty()) query.bindValue(":color", color);
query.exec();
query.prepare(QString("SELECT * FROM rabbits WHERE color") + (color.isEmpty() ? " IS NULL" : "=:color"));
if(!color.isEmpty()) query.bindValue(":color", color);
query.exec();
To copy to clipboard, switch view to plain text mode
Bookmarks