我有一組複選框代表我在Qt應用程序GUI中每週的幾天,我選擇了一天或多天,並且根據選中的框,通過查詢字符串到PostgreSQL,以便在那些日子顯示某些數據 - 例如如果我檢查星期一和星期三,應該將extract (dow from timestamp) = 1 or extract(dow from timestamp) = 3
添加到我的查詢中。我剛剛輸入了一個粗略的解決方案 - 儘管在我寫這篇文章的時候還沒有測試過,但我想知道是否有一個更短的,更優雅的方法,我錯過了這裏。代碼如下:-The queryAdditionCalltimePart
和queryAdditionCallStampPart
字符串後添加到我的主要查詢的QString
相關部分前主查詢是executed-從QT複選框到Postgresql的星期選擇日(日)
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(")");