Autor Beitrag
eherzel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22

XP
D5 Enterp
BeitragVerfasst: Mi 02.08.06 10:00 
Folgendes Problem:

query1 liefert mir eine Ergebnismenge zurück, welche mit einer while-Schleife durchlaufen wird. In dieser Schleife gibt es einen query2, welcher auch eine Ergebnismenge zurück liefert, die jeweiligen Daten möchte ich in einem Record ablegen. der erste Durchlauf von query1 funktioniert tadellos, beim zweiten Durchlauf jedoch stürzt mein Programm regelmäßig ab, sobald ich auf den Inhalt eines Feldes zugreifen will via FieldByName, das ganze läuft unter Delphi 5. Gibt es dazu einen bekannten Bug, das man keine Queries verschachteln darf? Beim Debuggen im Assembler bin ich soweit gekommen, daß GetFieldData wohl der Übeltäter ist, da er den Heap nicht ordentlich aufräumt!


eherzel
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mi 02.08.06 10:09 
Mir ist kein Fehler bei der Verschachtelung von Queries bekannt.
Wenn der Fehler erst beim zweiten Durchlauf entsteht, würde ich mal prüfen, ob du irgendwas frei gegeben hast, was du eigentlich noch brauchst.
Ansonsten ist es ohne Source etwas schwierig, genaueres zu sagen. Wobei der Bereich vor und nach der Schleife besonders wichtig ist.
eherzel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22

XP
D5 Enterp
BeitragVerfasst: Mi 02.08.06 10:31 
meine Records:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
TPROCESSDETAILS = Record
  szUser: string;
  dtBegin: TDateTime;
  dtEnd: TDateTime;
end;
TPROCESS = Record
  szProcess: string;
  details: array of TPROCESSDETAILS;
end;
TDOC = Record
  docidx: integer;
  szEndState: string;
  processes: array of TPROCESS;
end;



meine queries:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var
  fQuery, fQuery2, fQuery3: TQuery;
...
initialization
  fQuery := TQuery.Create(nil);
  fQuery.DatabaseName := 'fmfo';
  fQuery2 := TQuery.Create(nil);
  fQuery2.DatabaseName := 'fmfo';
  fQuery3 := TQuery.Create(nil);
  fQuery3.DatabaseName := 'fmfo';




und der Code:

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:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
procedure FillDocArray(nextDate: TDateTime);
var
  i, j, k: integer;
  szTmp: string;
begin
  // find all finished docs
  fQuery.Close;
  fQuery.SQL.Clear;
  fQuery.SQL.Add('SELECT DOCIDX, DOCSTATE FROM XDFDOC WHERE DOCSTATE IN ' +
    '(:param1) AND DOCLASTUPD >= :param2 AND DOCLASTUPD < :param3');
  fQuery.ParamByName('param1').AsString := lszStates.CommaText;
  fQuery.ParamByName('param2').AsDate := nextDate;
  fQuery.ParamByName('param3').AsDate := nextDate + 1;
  if not fQuery.Prepared then fQuery.Prepare;
  fQuery.Open;
  if fQuery.RecordCount > 0 then begin
    SetLength(docs, fQuery.RecordCount);
    i := 0;
    while (not fQuery.Eof) do begin
      docs[i].docidx := fQuery.FieldByName('DOCIDX').AsInteger;
      docs[i].szEndState := fQuery.FieldByName('DOCSTATE').AsString;
      frmMain.Edit1.Text := IntToStr(i) + ' - ' + IntToStr(docs[i].docidx);
      frmMain.Refresh;
      // find processes
      fQuery2.Close;
      fQuery2.SQL.Clear;
      fQuery2.SQL.Add('SELECT PROCESS, COUNT FROM DOC_PROCESSES_COUNT ' +
        ' WHERE DOCIDX = :param1');
      fQuery2.ParamByName('param1').AsInteger := docs[i].docidx;
      if not fQuery2.Prepared then fQuery2.Prepare;
      fQuery2.Open;
      if fQuery2.RecordCount > 0 then begin
        SetLength(docs[i].processes, fQuery2.RecordCount);
        j := 0;
        fQuery2.First;
        while (not fQuery2.Eof) do begin
          SetLength(docs[i].processes[j].details, fQuery2.FieldByName('COUNT').AsInteger);
          szTmp := fQuery2.FieldByName('PROCESS').AsString;
          docs[i].processes[j].szProcess := szTmp;
          // process details
          fQuery3.Close;
          fQuery3.SQL.Clear;
          fQuery3.SQL.Add('SELECT * FROM DOC_PROCESSES WHERE DOCIDX = :param1 ' +
            ' AND PROCESS = :param2 ORDER BY ''BEGIN''');
          fQuery3.ParamByName('param1').AsInteger := docs[i].docidx;
          fQuery3.ParamByName('param2').AsString := docs[i].processes[j].szProcess;
          if not fQuery3.Prepared then fQuery3.Prepare;
          fQuery3.Open;
          k := 0;
          fQuery3.First;
          while (not fQuery3.Eof) do begin
            docs[i].processes[j].details[k].szUser := fQuery3.FieldByName('USER').AsString;
            docs[i].processes[j].details[k].dtBegin := fQuery3.FieldByName('BEGIN').AsDateTime;
            docs[i].processes[j].details[k].dtEnd := fQuery3.FieldByName('END').AsDateTime;
            k := k + 1;
            fQuery3.Next;
          end;
          j := j + 1;
          fQuery2.Next;
        end;
      end;
      i := i + 1;
      fQuery.Next;
    end;
  end;
end;



Beim zweiten Durchlauf dann bricht er an dieser Stelle:
ausblenden Delphi-Quelltext
1:
      docs[i].szEndState := fQuery.FieldByName('DOCSTATE').AsString;					

mit einer AccessViolation ab!


eherzel
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mi 02.08.06 10:46 
Kann es sein, dass deine Variable docs ein dynamisches Array ist?
Dann ist der Fehler ziemlich klar. Ich sehe nämlich nirgendwo, dass du die Länge des Arrays festlegst. Somit greifst du beim zweiten Durchlauf auf ungültige Bereiche zu (docs[i]).
eherzel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22

XP
D5 Enterp
BeitragVerfasst: Mi 02.08.06 11:01 
ja es ist ein dynamisches Array und wird in Zeile 17 hiermit initialisiert:

SetLength(docs, fQuery.RecordCount);


Und würde ich es nicht initialisieren, hätte ich ja beim ersten Durchlauf schon ein Problem!
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mi 02.08.06 11:06 
Aah. Übersehen. Du darfst dich aber auf RecordCount nicht verlassen. Das liefert oft genug "0" zurück, auch wenn es mehr gibt. Lass dir anzeigen, was es ist und ich bin mir zu 99% sicher, es ist "0".

user profile iconeherzel hat folgendes geschrieben:
Und würde ich es nicht initialisieren, hätte ich ja beim ersten Durchlauf schon ein Problem!

Nicht zwingend. Unter Umständen kann es trotzdem im ersten Durchlauf funktionieren.
eherzel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22

XP
D5 Enterp
BeitragVerfasst: Mi 02.08.06 11:09 
RecordCount ist 415 und wäre er 0, würde er ja die Schleife erst gar nicht durchlaufen, weil Zeile 16:

if fQuery.RecordCount > 0 then begin
eherzel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22

XP
D5 Enterp
BeitragVerfasst: Mi 02.08.06 11:31 
desweiteren habe ich auch getestet, die einzelnen queries nacheinander auszuführen, allerdings geht dann die Verarbeitungszeit in den Minutenbereich, da ich dann verschachtelete Schleifen über die arrays machen muß und dies kann nicht der Weisheit letzter Schluß sein...
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mi 02.08.06 12:04 
Was du schreibst stimmt. Ich habe mir das jetzt nochmal ganz genau angesehen. Bisher konnte ich keinen Fehler erkennen. Lasse dir doch vor der problematischen Zeile alle notwendigen Informationen anzeigen. i, Arraygröße und Feldinhalt würden mich zunächst mal interessieren.
Ein bisschen merkwürdig ist das schon.
eherzel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22

XP
D5 Enterp
BeitragVerfasst: Mi 02.08.06 12:17 
hier mal zwei Screenshots
Einloggen, um Attachments anzusehen!
_________________
programming today is a race between software engineers striving to build bigger and better idiot-proof programs,
and the Universe trying to produce bigger and better idiots" - "So far, the Universe is winning..."
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mi 02.08.06 13:25 
Sieht zunächst korrekt aus. Was ist mit fQuery.FieldByName('DOCSTATE').AsString ?
MarkusB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Mi 02.08.06 13:30 
moin eherzel!

1. was steht in 'DOCSTATE'?
2. kannst Du die Fehlermeldung posten?

viele grüße
markus
eherzel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22

XP
D5 Enterp
BeitragVerfasst: Mi 02.08.06 14:01 
Problem gefunden und gelöst, Ursache ist eine Funktion aus einer DLL, welche ich mit eingebunden habe, diese habe ich falsch deklariert:

falsch:
ausblenden Delphi-Quelltext
1:
TIT_GetInfo = function ( nEntityType : integer; nEntityIndex : integer; pcCodeInfo : PChar; pcValue : PChar) : integer; cdecl;					


richtig:
ausblenden Delphi-Quelltext
1:
TIT_GetInfo = function ( nEntityType : integer; nEntityIndex : integer; pcCodeInfo : PChar; pcValue : PChar) : integer; stdcall;					


Die Verwendung dieser Funktion dann hat den Stack durcheinander gebracht, aber wie soll man darauf kommen, wenn das Problem irgendwo im Code auftritt...

_________________
programming today is a race between software engineers striving to build bigger and better idiot-proof programs,
and the Universe trying to produce bigger and better idiots" - "So far, the Universe is winning..."
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mi 02.08.06 14:04 
user profile iconeherzel hat folgendes geschrieben:
Die Verwendung dieser Funktion dann hat den Stack durcheinander gebracht, aber wie soll man darauf kommen, wenn das Problem irgendwo im Code auftritt...

Wieso? Du bist doch drauf gekommen. :zustimm:
eherzel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22

XP
D5 Enterp
BeitragVerfasst: Mi 02.08.06 14:39 
user profile iconjasocul hat folgendes geschrieben:
Wieso? Du bist doch drauf gekommen. :zustimm:


Nachdem ich alles in Einzelteile "zerstückelt" habe... :gruebel:

_________________
programming today is a race between software engineers striving to build bigger and better idiot-proof programs,
and the Universe trying to produce bigger and better idiots" - "So far, the Universe is winning..."
raiguen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 374

WIN 2000prof, WIN XP prof
D7EP, MSSQL, ABSDB
BeitragVerfasst: Mi 02.08.06 20:11 
Moin :-)

Auch wenn das Problem als solches schon gelöst ist, so darf ich vllt. noch eine kleine Anmerkung bzgl der Queries machen?
Der Sinn (oder Vorteil) einer parametrisierten Query ist doch der, dass die Abfrage zunächst vorbereitet wird und dann die Parameter mit entsprechenden Werten befüllt werden; vorzugsweise werden Parameter in einer Schleife o.ä. übergeben.

Auf Dein Codeschnippsel angewandt sähe es dann so aus:
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:
44:
45:
46:
47:
48:
49:
50:
51:
procedure FillDocArray(nextDate: TDateTime);
var
  i, j, k: integer;
  szTmp: string;
begin
  // find all finished docs
  fQuery.Close;
  fQuery.SQL.Clear;
  fQuery.SQL.Add('SELECT DOCIDX, DOCSTATE FROM XDFDOC WHERE DOCSTATE IN ' +
                 '(:param1) AND DOCLASTUPD >= :param2 AND DOCLASTUPD < :param3');
  fQuery.ParamByName('param1').AsString := lszStates.CommaText;
  fQuery.ParamByName('param2').AsDate := nextDate;
  fQuery.ParamByName('param3').AsDate := nextDate + 1;
  if not fQuery.Prepared then fQuery.Prepare;


  //--2.Query vorbereiten
  fQuery2.SQL.Clear;
  fQuery2.SQL.Add('SELECT PROCESS, COUNT FROM DOC_PROCESSES_COUNT ' +
                  ' WHERE DOCIDX = :param1');
  if not fQuery2.Prepared then fQuery2.Prepare;

  //--3.Query vorbereiten
  fQuery3.SQL.Clear;
  fQuery3.SQL.Add('SELECT * FROM DOC_PROCESSES WHERE DOCIDX = :param1 ' +
                  ' AND PROCESS = :param2 ORDER BY ''BEGIN''');
  if not fQuery3.Prepared then fQuery3.Prepare;


  fQuery.Open;  
  if fQuery.RecordCount > 0 then begin
    SetLength(docs, fQuery.RecordCount);
    i := 0;
    while (not fQuery.Eof) do begin
      ...
      // find processes
      fQuery2.Close;
      fQuery2.ParamByName('param1').AsInteger := docs[i].docidx;
      fQuery2.Open;

      if fQuery2.RecordCount > 0 then begin
        ...
        while (not fQuery2.Eof) do begin
          ...
          // process details
          fQuery3.Close;
          fQuery3.ParamByName('param1').AsInteger := docs[i].docidx;
          fQuery3.ParamByName('param2').AsString := docs[i].processes[j].szProcess;
          fQuery3.Open;
          ...
end;

Natürlich geht das auch so wie in Deinem 'Original'code, nur machst Du Dir den eigentlichen Vorteil - das einmalige Vorbereiten und dann nur die entsprechenden Parameterwerte übergeben - zunichte bzw führst es ad absurdum (nennt man das so??), weil Du jedesmal das Statement neu erstellest (dann könnstest Du auch gleich die Parameterwerte fest übergeben*gg*) und somit 'kostbare' Zeit verlierst - denn die Database (oder wer auch immer für das Vorbereiten und Ausführen einer Query zuständig ist) schmeißt die Anweisung jedesmal in die Tonne um anschließend die gleiche dann wieder neu aufzubauen, mit anderen Parameterwerten zu befüllen und auszuführen...Mit der einmaligen Vorbereitung wartet die Query eigentlich nur noch auf die Parameter... Ergo ist die Ausführungsgeschwindigkeit um einige Ticks besser ;-)

So, ich hoffe nun nicht allzu besserwisserich rüber gekommen zu sein...
eherzel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22

XP
D5 Enterp
BeitragVerfasst: Mi 02.08.06 20:54 
wieder was dazu gelernt, werd ich morgen gleich testen, danke dafür!!!

_________________
programming today is a race between software engineers striving to build bigger and better idiot-proof programs,
and the Universe trying to produce bigger and better idiots" - "So far, the Universe is winning..."
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 03.08.06 06:48 
@raiguen:
Das hängt auch von der Datenbank ab.
Ich kann dabei nur für Oracle sprechen. Wenn ich das richtig verstanden habe, werden die Abfragen client-unabhängig "irgendwo" zwischengespeichert. Ob beim Client dann die Abfrage jedesmal neu aufgebaut wird, ist dabei egal. Wichtig ist nur, dass parametrisiert sein muss. Ich hoffe, dass ich mich richtig erinnert habe. Ist schon ein paar Jahre her, dass ich mir das durchgelesen hatte.
Auf jeden Fall führt dein Änderungsvorschlag dazu, dass der Source erheblich übersichtlicher wird.
eherzel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22

XP
D5 Enterp
BeitragVerfasst: Do 03.08.06 12:11 
und funktionieren tut es auch, zwar konnte ich keinen Geschwindigkeitsvorteil erkennen, aber allein schon die bessere Strukturierung des Codes war ein Gewinn!

_________________
programming today is a race between software engineers striving to build bigger and better idiot-proof programs,
and the Universe trying to produce bigger and better idiots" - "So far, the Universe is winning..."