Seite 1 von 1

Aufwenige Schleife vereinfachen

Verfasst: 7. Juni 2007 08:36
von pfusterschmied
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)

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);
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?

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 +"");
									}
								}
							}
						}
					}
	
				}
			}
		}
	}

Verfasst: 7. Juni 2007 08:48
von upsala
1. Dein Beispiel ist unübersichtlich...
2. Bei 'SELECT *' ist die Reihenfolge und Anzahl der zurückgelieferten Parameter nicht definiert.
3. Indexe beginnen nicht zwangsweise mit 1.

Verfasst: 7. Juni 2007 09:03
von pfusterschmied
SELECT * liefert mir alle Einträge zurück.
Index beginnt mit 1 weil ich eine Relationale DB verwende.

Neues Bsp.:
Mit nur 2 Combos:

Code: Alles auswählen


(if combo_1->currentIndex() == 0 &&  combo_2->currentIndex() == 0)
{
	myQuery.exec("SELECT * FROM `tx_submission_data`");
}
else
{
	for( int z = 1; z < comboBox_1->count(); z++)
	{
		if (comboBox_1->currentIndex() == z && comboBox_2->currentIndex() == 0 )
			{	
				myQuery.exec("SELECT * FROM `tx_submission_data` WHERE `ep_date` = "+  comboBox_1->currentIndex() +"");
			} 
			else 
			{
				for( int y = 1; y < comboBox_PrintGender->count(); y++)
				{
					if (comboBox_1->currentIndex() == z && comboBox_2->currentIndex() == y)
					{	
						myQuery.exec("SELECT * FROM `tx_submission_data` WHERE  `ep_date` = "+ comboBox_1->currentIndex() +" AND `gender` = CONVERT( _utf8 '"+ comboBox_2->currentText() +"' USING latin1 ) COLLATE latin1_general_ci");
					}
				}
			}
	}
}




Lösung

Verfasst: 7. Juni 2007 18:43
von pfusterschmied
Hab eine Lösng gefunden!

Es lag einerseits an einer falschen MySQL Abfrage :oops: und ich habe alle for Schleifen bei 0 beginnen lassen und zuerst durchlaufen lassen und in den for Schleifen erst die if Abfrage gestellt.

Kurzes Bsp.:

Code: Alles auswählen

for(....)
{
	for(....)
	{
		for(....)
		{
			if (...)
			if (...)
		}
	}
}