Delphi-PRAXiS
Seite 1 von 3  1 23      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Delphi select anweisung (https://www.delphipraxis.net/172602-select-anweisung.html)

stathis 13. Jan 2013 20:15

Datenbank: dbexpress • Version: 5 • Zugriff über: -

select anweisung
 
hallo,
ich habe eine frage
wenn in eine select anweisung ein feld selectiere dann ist alles ok.
Also folgede code lauft ohne problem:
Delphi-Quellcode:
sqlquery1.sql.Clear;
SQLquery1.SQL.Text := 'Select sum(poso) as poso from history group by user';
sqlquery1.open;
    SQLquery1.First;
       while not SQLquery1.EOf do begin
                   stringgrid1.Cells[2,i] := floattostr(SQLquery1.Fieldbyname('poso').asfloat);
       SQLquery1.Next;
     i:=i+1;
     end;
aber bei der folgende code:
Delphi-Quellcode:
sqlquery1.sql.Clear;
SQLquery1.SQL.Text := 'Select user,sum(poso) as poso from history group by user';
sqlquery1.open;
    SQLquery1.First;
       while not SQLquery1.EOf do begin
                   stringgrid1.Cells[3,i] := SQLquery1.Fieldbyname('user').asstring;
                   stringgrid1.Cells[2,i] := floattostr(SQLquery1.Fieldbyname('poso').asfloat);
       SQLquery1.Next;
     i:=i+1;
     end;
kann nicht das feld 'poso' nicht finden.

wieso?

Entschuldingen sie mir fuer meine deutsche Sprache

Medium 14. Jan 2013 00:46

AW: select anweisung
 
If English is easier for you, it is perfectly okay to ask in English here. I'll continue in it, presuming that it will at least yield better results for you when running it through a translator.

It might be, that the field name "user" is considered a reserved word by your database engine, and the select statement fails or produces unexpected results. Escaping it and maybe giving it an alias could help. I am not entirely sure though, because I have no experience with dbexpress. If you have some external querying tool, trying to execute that exact statement might also reveal what is wrong with it, or what names the fields are given to in the end.

r_kerber 14. Jan 2013 05:49

AW: select anweisung
 
Try this
Delphi-Quellcode:
sqlquery1.sql.Clear;
SQLquery1.SQL.Text := 'Select his.user user_,sum(his.poso) as poso from history his group by his.user';
sqlquery1.open;
SQLquery1.First;
while not SQLquery1.EOf do begin
stringgrid1.Cells[3,i] := SQLquery1.Fieldbyname('user_').asstring;
stringgrid1.Cells[2,i] := floattostr(SQLquery1.Fieldbyname('poso').asfloat);
SQLquery1.Next;
i:=i+1;
end;

stathis 14. Jan 2013 19:51

AW: select anweisung
 
Es hat leider nicht funktioniert. Ich bekomme immer wieder das gleiche Fehler: ‘Field poso not found’
Ich kann nur das erste Field rausnehme und bei der 2. Bekomme ich die Fehler Meldung.
Also wenn das Code so ist:
sqlquery1.sql.Clear;
SQLquery1.SQL.Text := 'Select sum(his.poso) as poso, his.user user_ from history his group by his.user';
sqlquery1.open;
SQLquery1.First;
while not SQLquery1.EOf do begin
stringgrid1.Cells[2,i] := floattostr(SQLquery1.Fieldbyname('poso').asfloat);
stringgrid1.Cells[3,i] := SQLquery1.Fieldbyname('user_').asstring;
SQLquery1.Next;
i:=i+1;
end;

Dann bekomme folgende Fehler : ‘Field user_ not found‘
Ich danke Ihnen
Stathis

Volker Z. 15. Jan 2013 00:52

AW: select anweisung
 
Hallo,

nutzt Du MySQL 5.x und dbxopenmysql5?

Wenn ja, hast Du auch ein
Delphi-Quellcode:
uses
  dbxopenmysql50;
in Deinem Quellcode?

Gruß

sx2008 15. Jan 2013 06:06

AW: select anweisung
 
Also ich würde einem Ausdruck (sum(poso)) nicht den gleichen Namen geben wie einem existierenden Feld.
Daher:
SQL-Code:
SELECT SUM(poso) AS Sumposo FROM ....

Ausserdem würde ich das Feld User umbenennen, weil du mit diesen reservierten Namen immer wieder Schwierigkeiten bekommst.
Kleines Beispiel:
Delphi-Quellcode:
function Test:Integer;
var
  // es ist zwar erlaubt eine Variable "published" zu nennen, aber dennoch eine schlechte Idee
  // weil dieser Bezeichner in Delphi schon für einen anderen Zweck reserviert ist
  published : Integer;
begin
Ein guter Delpih-Programmierer würde es vermeiden eine Variable "published" zu nennen.
Genauso würde ein guter Datenbankdesigner es vermeiden ein Feld "user" zu nennen.

stathis 15. Jan 2013 12:45

AW: select anweisung
 
Zitat:

Zitat von Volker Z. (Beitrag 1199052)
Hallo,

nutzt Du MySQL 5.x und dbxopenmysql5?

Wenn ja, hast Du auch ein
Delphi-Quellcode:
uses
  dbxopenmysql50;
in Deinem Quellcode?

Gruß

neis das habe ich nicht gehabt, aber bekomme folgende fehler:
[Fatal Error] Unit2.pas(8): File not found: 'dbxopenmysql50.dcu'

(in instalation verzeichnis habe ich nur dbxopenmysql50.dll und nicht dbxopenmysql50.dcu)

Volker Z. 15. Jan 2013 13:23

AW: select anweisung
 
Hallo,

Du müsstest in einem Verzeichnis folgende Dateien haben: dbxopenmysql50.pas, dbxMysql50.pas, PlainMysql50 und SqlxApi.pas. Ergänze Deine Suchpfade für Units um den entsprechenden Pfad.

Aus der install.txt
Zitat:

In order to static-link this driver as a DCU, put the compiled DCUs somewhere on your Unit search Path, and add dbxopenmysql50 to your uses clause.
Gruß

gro 15. Jan 2013 13:26

AW: select anweisung
 
versuchs mal so:
Code:
  ADOQuery1.SQL.Text := 'SELECT user, sum(poso) as sum FROM history group by user';
  ADOQuery1.Active := TRUE;

  iCount := 1;

  while (ADOQuery1.Eof = FALSE) do begin
    stringgrid1.Cells[3, iCount] := ADOQuery1.Fieldbyname('user').asstring;
    stringgrid1.Cells[2, iCount] := floattostr(ADOQuery1.Fieldbyname('sum').asfloat);

    ADOQuery1.Next;
    inc (iCount);
  end;

stathis 15. Jan 2013 17:32

AW: select anweisung
 
Zitat:

Zitat von Volker Z. (Beitrag 1199140)
Hallo,

Du müsstest in einem Verzeichnis folgende Dateien haben: dbxopenmysql50.pas, dbxMysql50.pas, PlainMysql50 und SqlxApi.pas. Ergänze Deine Suchpfade für Units um den entsprechenden Pfad.

Aus der install.txt
Zitat:

In order to static-link this driver as a DCU, put the compiled DCUs somewhere on your Unit search Path, and add dbxopenmysql50 to your uses clause.
Gruß

ich habe alles gemacht. Ich bekomme immer wieder das gleiche Fehler;:(


Alle Zeitangaben in WEZ +1. Es ist jetzt 03:12 Uhr.
Seite 1 von 3  1 23      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz