Aufwenige Schleife vereinfachen
Verfasst: 7. Juni 2007 08:36
Hi Leute,
Hab ein totales Anfängerproblem und mir fehlt mittlerweile der totale Durchblick für das Wesentliche. Ich habe 4 QCombos welche mit mit Daten aus der DB gefüttert werden:
(Hier nur eine dargestellt)
Nun möchte ich mittels dieser 4 QCombos meine SQL Abfrage generieren. Wenn ich nun für jeden Combozustand eine IF Abfrage machen würde brauche ich ca. 234 IF's. Ausserdem wäre das Programm nicht mehr dynamisch. Daher habe ich an eine Mischung aus if und for gedacht, leider funktioniert dies nicht ganz.
So, falls mir noch jemand folgen kann, hier meine Frage:
1.) Gibts für diese Art der Abfrage eine bessere Art der Schleifenbildung?
2.) Jede ComboBox hat als Item0 eine Art WildCard (sprich die Combo wird ignoriert indem das Element im SQL String nicht eingefügt wird ). Das Funktioniert jedoch nur sollangeg man die Combos in richtiger Reihenfolge aktiviert(Indexwerte: 1234 z.B.). In der MySQL Db beginnen die Einträge mit Index == 1 ). Somit liefert eine Abfrage mit ComboItem==0 nichts zurück! Wie könnte man dies lösen?
Hab ein totales Anfängerproblem und mir fehlt mittlerweile der totale Durchblick für das Wesentliche. Ich habe 4 QCombos welche mit mit Daten aus der DB gefüttert werden:
(Hier nur eine dargestellt)
Code: Alles auswählen
// EP Term Combos füllen
QSqlQuery myEPTermQuery;
myEPTermQuery.exec("SELECT * FROM `tx_submission_epterm`");
while (myEPTermQuery.next())
{
EPTerm += myEPTermQuery.value(1).toString();
}
comboBox_printEpTerm->addItems(EPTerm);
So, falls mir noch jemand folgen kann, hier meine Frage:
1.) Gibts für diese Art der Abfrage eine bessere Art der Schleifenbildung?
2.) Jede ComboBox hat als Item0 eine Art WildCard (sprich die Combo wird ignoriert indem das Element im SQL String nicht eingefügt wird ). Das Funktioniert jedoch nur sollangeg man die Combos in richtiger Reihenfolge aktiviert(Indexwerte: 1234 z.B.). In der MySQL Db beginnen die Einträge mit Index == 1 ). Somit liefert eine Abfrage mit ComboItem==0 nichts zurück! Wie könnte man dies lösen?
Code: Alles auswählen
// Combos abfragen und Query erstellen und DB abfragen
if (comboBox_printEpTerm->currentIndex() == 0 && comboBox_PrintGender->currentIndex() == 0 && comboBox_printEpSf->currentIndex() == 0 && comboBox_printEpStatus->currentIndex() == 0)
{
myQuery.exec("SELECT * FROM `tx_submission_data` "+ sorting +" ");
}
else
{
for( int z = 1; z < comboBox_printEpTerm->count(); z++)
{
QString epTerm;
epTerm.setNum(z);
if (comboBox_printEpTerm->currentIndex() == z && comboBox_PrintGender->currentIndex() == 0 && comboBox_printEpSf->currentIndex() == 0 && comboBox_printEpStatus->currentIndex() == 0)
{
myQuery.exec("SELECT * FROM `tx_submission_data` WHERE `ep_date` = CONVERT( _utf8 '"+ epTerm +"' USING latin1 ) COLLATE latin1_general_ci "+ sorting +"");
qDebug() << ("SELECT * FROM `tx_submission_data` WHERE `ep_date` = CONVERT( _utf8 '"+ epTerm +"' USING latin1 ) COLLATE latin1_general_ci "+ sorting +"");
}
else
{
for( int y = 1; y < comboBox_PrintGender->count(); y++)
{
if (comboBox_printEpTerm->currentIndex() == z && comboBox_PrintGender->currentIndex() == y && comboBox_printEpSf->currentIndex() == 0 && comboBox_printEpStatus->currentIndex() == 0)
{
myQuery.exec("SELECT * FROM `tx_submission_data` WHERE `gender` = CONVERT( _utf8 '"+ comboBox_PrintGender->currentText() +"' USING latin1 ) COLLATE latin1_general_ci AND `ep_date` = CONVERT( _utf8 '"+ comboBox_printEpTerm->currentText() +"' USING latin1 ) COLLATE latin1_general_ci "+ sorting +"");
qDebug() << ("SELECT * FROM `tx_submission_data` WHERE `gender` = CONVERT( _utf8 '"+ comboBox_PrintGender->currentText() +"' USING latin1 ) COLLATE latin1_general_ci AND `ep_date` = CONVERT( _utf8 '"+ comboBox_printEpTerm->currentText() +"' USING latin1 ) COLLATE latin1_general_ci "+ sorting +"");
}
else
{
for( int x = 1; x < comboBox_printEpSf->count(); x++)
{
QString sf;
sf.setNum(x);
if (comboBox_printEpTerm->currentIndex() == z && comboBox_PrintGender->currentIndex() == y && comboBox_printEpSf->currentIndex() == x && comboBox_printEpStatus->currentIndex() == 0)
{
myQuery.exec("SELECT * FROM `tx_submission_data` WHERE `gender` = CONVERT( _utf8 '"+ comboBox_PrintGender->currentText() +"' USING latin1 ) COLLATE latin1_general_ci AND `ep_date` = CONVERT( _utf8 '"+ comboBox_printEpTerm->currentText() +"' USING latin1 ) COLLATE latin1_general_ci AND `sf`="+ sf + " " + sorting +" ");
qDebug() << ("SELECT * FROM `tx_submission_data` WHERE `gender` = CONVERT( _utf8 '"+ comboBox_PrintGender->currentText() +"' USING latin1 ) COLLATE latin1_general_ci AND `ep_date` = CONVERT( _utf8 '"+ comboBox_printEpTerm->currentText() +"' USING latin1 ) COLLATE latin1_general_ci AND `sf`="+ sf + " " + sorting +" ");
}
else
{
for( int w = 1; w < comboBox_printEpStatus->count(); w++)
{
if (comboBox_printEpTerm->currentIndex() == z && comboBox_PrintGender->currentIndex() == y && comboBox_printEpSf->currentIndex() == x && comboBox_printEpStatus->currentIndex() == w)
{
QString status;
status.setNum(w);
myQuery.exec("SELECT * FROM `tx_submission_data` WHERE `gender` = CONVERT( _utf8 '"+ comboBox_PrintGender->currentText() +"' USING latin1 ) COLLATE latin1_general_ci AND `ep_date` = CONVERT( _utf8 '"+ comboBox_printEpTerm->currentText() +"' USING latin1 ) COLLATE latin1_general_ci AND `sf`="+ sf +" AND `status`="+ status + " " + sorting +"");
qDebug() << ("SELECT * FROM `tx_submission_data` WHERE `gender` = CONVERT( _utf8 '"+ comboBox_PrintGender->currentText() +"' USING latin1 ) COLLATE latin1_general_ci AND `ep_date` = CONVERT( _utf8 '"+ comboBox_printEpTerm->currentText() +"' USING latin1 ) COLLATE latin1_general_ci AND `sf`="+ sf +" AND `status`="+ status + " " + sorting +"");
}
}
}
}
}
}
}
}
}