Autor Beitrag
D. Annies
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 02.07.09 20:49 
Hi, Delpher,

nochmal ganz herzlichen Dank für eure Hilfe bei meinem letzten Thread!

Ich muss also anders vorgehen (schade).

Jetzt geht es um folgende (fehlerhafte) Query:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
  query1.Close;
    query1.SQL.Text :=
      format('select max(punkte) as maxpkt from "%s" S ' +
             'where (geschlecht = ''w'') and (klasse like "%s" ) ', [Table2.tablename, '''%'+listbox1.items[n]+'''%']);
  query1.Open;


Es geht vieleicht nur um die Anzahl der Anführungszeichen. (?)
Die Fehlermeldung ist: '' ist kein gültiger Integerwert.

Die Klausel [Table2.tablename, '%7%'] funktioniert übrigens!

Habt ihr eine Idee?

Danke, Detlef

_________________
ut vires desint, tamen est laudanda voluntas
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19340
Erhaltene Danke: 1752

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 02.07.09 21:13 
Sind die Anführungszeichen nicht zu viel? :gruebel:
user profile iconD. Annies hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden Delphi-Quelltext
1:
2:
3:
    query1.SQL.Text :=
      format('select max(punkte) as maxpkt from "%s" S ' +
             'where (geschlecht = ''w'') and (klasse like "%s" ) ', [Table2.tablename, '''%'+listbox1.items[n]+'''%']);
Bzw. wenn dann nicht symmetrisch gesetzt.
Niko S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 566
Erhaltene Danke: 10

Win 7, Ubuntu
Lazarus, Turbo Delphi, Delphu 7 PE
BeitragVerfasst: Do 02.07.09 21:13 
ausblenden Delphi-Quelltext
1:
'''%'+listbox1.items[n]+'%'''])					

So rum wäre das % schonmal besser.
Aber listbox1.items[n] liefert doch soweit ich weiß keinen integerwert zurück?

//edit
zu lahm :x
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 02.07.09 21:26 
Danke, ihr zwei, ich hab's jetzt gesehen.

By the way: Nachdem der Weg von gestern nicht ging, habe ich jetzt den folgenden Weg genommen, und der geht:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
n := 2; k := 2;
  repeat
    query1.Close;
      query1.SQL.Text :=
        format('select max(punkte) as maxpkt from "%s" S ' +
               'where (geschlecht = ''w'') and (klasse like "%s" ) ', [Table2.tablename, ''+listbox1.items[k]+'%']);
    query1.Open;
    showmessage('Mädchen: '+listbox1.Items[k]+ inttostr(query1.RecordCount));

    query1.First;
    stringgrid1.Cells[0,n] := inttostr(n);
    stringgrid1.Cells[1,n] := query1.fieldbyname('maxPkt').asstring;

    table2.locate('punkte', Query1.fieldbyname('maxpkt').asstring, []);

    stringgrid1.Cells[2,n] := table2.fieldbyname('Geschlecht').asstring;
    stringgrid1.Cells[3,n] := table2.fieldbyname('Klasse').asstring;
    stringgrid1.Cells[4,n] := table2.fieldbyname('Name').asstring;
    stringgrid1.Cells[5,n] := table2.fieldbyname('Vorname').asstring;
    inc(n);

    query1.Close;
      query1.SQL.Text :=                                                                // '7%' geht
        format('select max(punkte) as maxpkt from "%s" S ' +
               'where (geschlecht = ''m'') and (klasse like "%s") ', [Table2.tablename, ''+listbox1.items[k]+'%']);
    query1.Open;
    showmessage('Jungen: '+listbox1.Items[k]+inttostr(query1.RecordCount));

    query1.First;
    stringgrid1.Cells[0,n] := inttostr(n);
    stringgrid1.Cells[1,n] := query1.fieldbyname('maxPkt').asstring;

    table2.locate('punkte', Query1.fieldbyname('maxpkt').asstring, []);

    stringgrid1.Cells[2,n] := table2.fieldbyname('Geschlecht').asstring;
    stringgrid1.Cells[3,n] := table2.fieldbyname('Klasse').asstring;
    stringgrid1.Cells[4,n] := table2.fieldbyname('Name').asstring;
    stringgrid1.Cells[5,n] := table2.fieldbyname('Vorname').asstring;
    inc(n);
    inc(k);  //!!!
  until k = listbox1.Items.Count;

  stringgrid1.RowCount := n;


Mag "umständlich" erscheinen, aber verständlich, und funzt!!

Gruß, Detlef (der tapfere :D )

_________________
ut vires desint, tamen est laudanda voluntas
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19340
Erhaltene Danke: 1752

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 02.07.09 21:32 
user profile iconD. Annies hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden Delphi-Quelltext
1:
2:
format('select max(punkte) as maxpkt from "%s" S ' +
  'where (geschlecht = ''w'') and (klasse like "%s" ) ', [Table2.tablename, ''+listbox1.items[k]+'%']);
Das kannst du dir auch sparen, das bewirkt rein gar nichts. ;-)
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 02.07.09 22:15 
UPS, danke, Sebastian. [Hast mal wieder genau hingeguckt, aber alles andere bringt sowieso nichts...]

P.S. Die obige Lösung hat noch den "Nachteil", dass die gefundene Person zwar die Punktzahl hat, aber nicht aus der (richtigen) Klasse stammt. :evil:

Detlef

_________________
ut vires desint, tamen est laudanda voluntas
jfheins
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 918
Erhaltene Danke: 158

Win 10
VS 2013, VS2015
BeitragVerfasst: Do 02.07.09 22:26 
user profile iconD. Annies hat folgendes geschrieben Zum zitierten Posting springen:
P.S. Die obige Lösung hat noch den "Nachteil", dass die gefundene Person zwar die Punktzahl hat, aber nicht aus der (richtigen) Klasse stammt. :evil:

Liegt evtl. am like - das lässt ja mehrere klassen zu - geht's vll. wenn du das durch eine Gleichheitsbedingung ersetzt?
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19340
Erhaltene Danke: 1752

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 02.07.09 22:31 
Also ich habe mit MySQL nicht so viel gemacht bisher, aber ginge es nicht einfach so?
ausblenden Delphi-Quelltext
1:
2:
Format('select max(punkte) as maxpkt from "%s" ' +
  'where geschlecht = "w" and klasse like "%s";', [Table2.TableName, ListBox1.Items[k] + '%']);
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 02.07.09 22:43 
Also, ich meinte: der Query-Befehl ist so schon ok, aber es kann sein, dass die mit LOCATE gefundene Klasse nicht die richtige ist, wenn nämlich 2 Personen (gleichen Geschlechts) auch genau die gleiche Punktzahl haben.

Detlef

_________________
ut vires desint, tamen est laudanda voluntas
jfheins
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 918
Erhaltene Danke: 158

Win 10
VS 2013, VS2015
BeitragVerfasst: Do 02.07.09 22:48 
Könntest du nicht 2 Queries verwenden?

also eine:

SELECT max( punkte ), geschlecht, klasse
FROM pupils
GROUP BY klasse, geschlecht

damit solltest du zu jeder klasse und jedem geschlecht die beste Punktzahl heruasfinden. Und dann für jede Ergebnszeile sowas:

select * from pupils where punkte=query1.punkte and geschlecht=query1.geschlecht and klasse=query1.klasse

falls du da mehrere rausbekommst, sind die beiden gleich gut - gibt also keinen Besten :P
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 02.07.09 22:51 
Danke dir, probiere ich aber erst morgen aus und melde mich dann wieder. Muss jetzt in die Heia!

Detlef

_________________
ut vires desint, tamen est laudanda voluntas
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Fr 03.07.09 07:31 
Wie schon öfters bemerkt, solltest du dir wirklich mal die Verwendung von (SQL-)Parametern anschauen.

_________________
Markus Kinzler.
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Fr 03.07.09 12:33 
Und sollte das aus irgendwelchen Gründen nicht möglich sein, zumindest QuotedStr verwenden.
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Sa 04.07.09 10:06 
Danke!
Ich werde mir mal Beispiele mit quotedstr ansehen.

Gruß, Detlef

_________________
ut vires desint, tamen est laudanda voluntas
pto1
Hält's aus hier
Beiträge: 1



BeitragVerfasst: So 05.07.09 10:32 
probier mal die Schreibweisen

query1.SQL.Text := format(
'select max(punkte) as maxpkt from %s where (geschlecht = ''w'') and (klasse like ''%%%s%%'' ) ',
[Table2.tablename, listbox1.items[n]]);

oder mit Parametern
query1.SQL.Text := format(
'select max(punkte) as maxpkt from %s where (geschlecht = ''w'') and (klasse like :klasse ) ',
[Table2.tablename]);
query.params[0].asString:= '%'+ listbox1.items[n]+ '%';

Der Alias S für die Tabelle ist überflüssig, wird ja nirgendwo in der Query verwendet.
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 05.07.09 11:03 
ausblenden Delphi-Quelltext
1:
2:
query1.SQL.Text :=  format('select max(punkte) as maxpkt from %s where (geschlecht = %s) and (klasse like %s)'
  [Table2.tablename, QuotedStr('w'),QuotedStr(listbox1.items[n] + '%')]);
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Di 07.07.09 20:18 
Vielen Dank für die "Nachsorge",
Detlef

_________________
ut vires desint, tamen est laudanda voluntas
delphijanka
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 151

WinXP
D 2009 Arc., Java, C
BeitragVerfasst: Mi 05.08.09 15:11 
Problem:

Text: String;
...
Text := 'SELECT x FROM y WHERE x = "xy" ';

Wenn x eine String-Spalte in der DB ist (zb VARCHAR), welche ""-zeichen soll ich nutzen?

Danke.
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 05.08.09 15:22 
Am besten gar keine, sondern wie bereits erwähnt SQL-Parameter. Oder wie auch erwähnt QuotedStr verwenden, das sollte passen.
delphijanka
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 151

WinXP
D 2009 Arc., Java, C
BeitragVerfasst: Do 06.08.09 15:49 
Habe gerade etwas recherchiert und doch auf die Lösung gekommen, die aber imo beschäment ist (fu Delphi)

var
str: String;
sql: String;

str := 'blabla';

sql := 'SELECT * FROM Tabelle WHERE Attribut = ' + '''' + str + '''';

(also ' - Zeichen 4 mal auf jeder Seite)