我在我的 Qt 应用程序 GUI 中有一组代表一周中的几天的复选框,我选择一个或多个天,并根据选中的框,将查询字符串传递给 PostgreSQL 以显示那些日子的某些数据-例如如果我检查了周一和周三,extract (dow from timestamp) = 1 or extract(dow from timestamp) = 3
应该添加到我的查询中。我刚刚输入了一个粗略的解决方案——虽然在我写这篇文章时还没有测试过——但我想知道是否有一种更短、更优雅的方法我在这里错过了。代码如下: -queryAdditionCalltimePart
和字符串稍后会在主查询执行之前queryAdditionCallStampPart
添加到我的主查询的相关部分 -QString
bool checkboxArray[7];
bool mult = false;
checkboxArray[0] = this->ui->checkBoxMonday->isChecked();
checkboxArray[1] = this->ui->checkBoxTuesday->isChecked();
checkboxArray[2] = this->ui->checkBoxWednesday->isChecked();
checkboxArray[3] = this->ui->checkBoxThursday->isChecked();
checkboxArray[4] = this->ui->checkBoxFriday->isChecked();
checkboxArray[5] = this->ui->checkBoxSaturday->isChecked();
checkboxArray[6] = this->ui->checkBoxSunday->isChecked();
QString queryAdditionCalltimePart = "";
QString queryAdditionCalStampPart = "";
int count = 0;
queryAdditionCalltimePart.append("(");
queryAdditionCalStampPart.append("(");
for(int i = 0; i < 7; i++)
{
if(checkboxArray[i] == true)
{
count++;
}
}
int x = 0;
for(int i = 0; i < 7; i++)
{
if(checkboxArray[i] == true)
{
queryAdditionCalltimePart.append("(SELECT EXTRACT(DOW FROM calltime) = '" +QString::number(i+1)+"')");
queryAdditionCalStampPart.append("(SELECT EXTRACT(DOW FROM cal.stamp) = '" +QString::number(i+1)+"')");
}
if(count > 1 && checkboxArray[i] == true)
{
if(x == count - 1)
{
}
else
{
queryAdditionCalltimePart.append("OR");
queryAdditionCalStampPart.append("OR");
x++;
}
}
}
queryAdditionCalltimePart.append(")");
queryAdditionCalStampPart.append(")");