AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Datenbanken Delphi Counting in a database table
Thema durchsuchen
Ansicht
Themen-Optionen

Counting in a database table

Ein Thema von danten · begonnen am 30. Nov 2012 · letzter Beitrag vom 30. Nov 2012
Antwort Antwort
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#1

Counting in a database table

  Alt 30. Nov 2012, 08:59
Datenbank: Absolute Database • Version: 7.1 • Zugriff über: Absolute Database
Hello.
I need advice on how to count multiple entries in a database table from the third column to the last column in the whole table.
I need to count how many times the table number 1 and 10.
I use this code and it is not good:
Delphi-Quellcode:
procedure Tfrm_main._NE;
var
  i,y: integer;
begin
  Application.ProcessMessages;
with Table do
  begin
    DisableControls;
    y := 0;
    try
      First;
    while not EOF do
    begin
    for I := Table.FieldDefs.Count -1 downto 2 do
    begin
    if Pos('1', Table.FieldByName(Table.FieldDefs[i].Name).AsString) > 0 then
    begin
      y := y + 1;
      Label1.Caption := IntToStr(y) + ' x';
      Application.ProcessMessages;
    end;
    if Pos('10', Table.FieldByName(Table.FieldDefs[i].Name).AsString) > 0 then
    begin
      y := y + 1;
      Label2.Caption := IntToStr(y) + ' x';
      Application.ProcessMessages;
    end;
    end;
     Next;
     Application.ProcessMessages;
    end;
    finally
      EnableControls;
      Application.ProcessMessages;
    end;
  end;
The result is always the same!
Daniel
  Mit Zitat antworten Zitat
jobo

Registriert seit: 29. Nov 2010
3.072 Beiträge
 
Delphi 2010 Enterprise
 
#2

AW: Counting in a database table

  Alt 30. Nov 2012, 09:08
Why do You use same counting variable vor 1 and 10?
Is it intended to double count 1 if there is a 10?

Why don't You use a query, to do the count?
Gruß, Jo

Geändert von jobo (30. Nov 2012 um 09:12 Uhr)
  Mit Zitat antworten Zitat
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#3

AW: Counting in a database table

  Alt 30. Nov 2012, 09:19
Column names are not known in advance.
Now counts correctly.
y := 0;
y1 := 0;
for I := Table.FieldDefs.Count -1 downto 2 do
begin
if Pos('1', Table.FieldByName(Table.FieldDefs[i].Name).AsString) > 0 then
begin
y := y + 1;
Label1.Caption := IntToStr(y) + ' x';
Application.ProcessMessages;
end;
if Pos('10', Table.FieldByName(Table.FieldDefs[i].Name).AsString) > 0 then
begin
y1 := y1 + 1;
Label2.Caption := IntToStr(y1) + ' x';
Application.ProcessMessages;
end;
end;
Daniel
  Mit Zitat antworten Zitat
Benutzerbild von p80286
p80286

Registriert seit: 28. Apr 2008
Ort: Stolberg (Rhl)
6.659 Beiträge
 
FreePascal / Lazarus
 
#4

AW: Counting in a database table

  Alt 30. Nov 2012, 09:36
please give us an example of your data in the Table..

vergisses..
Programme gehorchen nicht Deinen Absichten sondern Deinen Anweisungen
R.E.D retired error detector
  Mit Zitat antworten Zitat
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#5

AW: Counting in a database table

  Alt 30. Nov 2012, 09:56
OK, here's a picture.
Miniaturansicht angehängter Grafiken
table.png  
Daniel
  Mit Zitat antworten Zitat
Benutzerbild von p80286
p80286

Registriert seit: 28. Apr 2008
Ort: Stolberg (Rhl)
6.659 Beiträge
 
FreePascal / Lazarus
 
#6

AW: Counting in a database table

  Alt 30. Nov 2012, 10:07
first it might be better to use Integer-Fields thean String Fields, to avoid double counting.

second all Fields inthe Table should be defined and knowed, so you better use a Query like
Code:
select field1,field2,field3...
frommy mytable
where field1=1
  or field2=1
  or ....
  or fieldx=10
that shoudn't be the fastes way but better than reading thousends of records for a dozend records which really count.

Delphi-Quellcode:
procedure Tfrm_main._NE;
var
  i: integer;
  cnt1 : integer;
  cnt10: integer;
begin
  Application.ProcessMessages;
with Table do
  begin
    DisableControls;
    cnt1:=0;
    cnt10:=0;
    try
      First;
      while not EOF do
      begin
        for I := Table.FieldDefs.Count -1 downto 2 do
        begin
          case Table.FieldByName(Table.FieldDefs[i].Name).AsInteger of
            1 : begin
                  cnt1:= cnt1 + 1;
                  Label1.Caption := IntToStr(cnt1) + ' x';
                  Application.ProcessMessages;
                end;
           10 : begin
                  cnt10 := cnt10 + 1;
                  Label2.Caption := IntToStr(cnt10) + ' x';
                  Application.ProcessMessages;
                end;
        end;
        Next;
        Application.ProcessMessages;
      end;
    finally
      EnableControls;
      Application.ProcessMessages;
    end;
  end;
K-H
Programme gehorchen nicht Deinen Absichten sondern Deinen Anweisungen
R.E.D retired error detector

Geändert von p80286 (30. Nov 2012 um 10:13 Uhr)
  Mit Zitat antworten Zitat
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#7

AW: Counting in a database table

  Alt 30. Nov 2012, 10:12
OK.
How then display the result in Label.Caption?
Daniel
  Mit Zitat antworten Zitat
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#8

AW: Counting in a database table

  Alt 30. Nov 2012, 10:31
first it might be better to use Integer-Fields thean String Fields, to avoid double counting.

second all Fields inthe Table should be defined and knowed, so you better use a Query like

Delphi-Quellcode:

        for I := Table.FieldDefs.Count -1 downto 2 do
        begin
          case Table.FieldByName(Table.FieldDefs[i].Name).AsInteger of
            1 : begin
                  cnt1:= cnt1 + 1;
                  Label1.Caption := IntToStr(cnt1) + ' x';
                  Application.ProcessMessages;
                end;
           10 : begin
                  cnt10 := cnt10 + 1;
                  Label2.Caption := IntToStr(cnt10) + ' x';
                  Application.ProcessMessages;
                end;
K-H
Thank you, now it counts quickly and correctly.
I somehow forgot the Case:
Daniel
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 09:21 Uhr.
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