Autor Beitrag
bflegel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 193
Erhaltene Danke: 1

Win XP, Win 7, BS2000
D5
BeitragVerfasst: Sa 15.12.07 03:09 
Hallo,

ich habe eine Verständnisfrage zur Verbindung ZQuery (Zeoslib) - Datasource - DBGrid:

Über eine ZConnection stelle ich eine Verbindung zu einer embedded Firebird DB her. Über die ZQuery1 (Property Active ist im OI "false") kann ich Tabellen erstellen und Werte in die DB schreiben, ohne die Property per Code auf "true" zu setzen.

Wenn ich die Daten der DB im DBGrid anzeigen lassen will
ausblenden Delphi-Quelltext
1:
ZQuery1.SQL.ADD('select * from gesundheit;');					

dann muss ich noch
ausblenden Delphi-Quelltext
1:
ZQuery1.Active := true;					

setzen, sonst wird im Grid nichts angezeigt.

Kann mir jemand erklären warum ich es einmal explizit aktivieren muss, und beim anderen nicht? Bin etwas ratlos.

Vielen Dank.

bflegel

_________________
I know all the jokes about my name
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Sa 15.12.07 04:05 
Hallo!

Dass du es manchmal nicht aktivieren musst, ist eine Eigenheit von Zeos. Normal ist das jedenfalls nicht. :o

Wenn du Operationen auf der Datenmenge machen willst, muss man die normalerweise immer explizit öffnen. Das Grid (beziehungsweise die Datasource vorher schon) verhält sich da wie man es erwartet und versucht bei leerer (=nicht offener) Datenmenge gar nicht erst etwas darzustellen.

Wieso man bei Zeos auf einer geschlossenen Tabelle arbeiten kann weiß ich allerdings nicht. Möglicherweise hat das was mit dem FB Embedded zu tun. Da wird das eventuell nicht so eng gesehen. Ich würde ja nachgucken, hab aber grad festgestellt dass ich die Zeoslib noch nicht wieder installiert hab...

Martok

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
bflegel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 193
Erhaltene Danke: 1

Win XP, Win 7, BS2000
D5
BeitragVerfasst: Sa 15.12.07 13:43 
Hallo Martok,

Danke für Deine Antwort. Dann werde ich mir angewöhnen grundsätzlich die Property zuerst auf false und danach auf true zu setzen.

Ein schönes Wochenende wünsche ich.

bflegel

_________________
I know all the jokes about my name
Siluro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 74

Win XP Prof., Win XP64 Prof., Win 7 Prof.
Delphi 7 Ent., Delphi XE, Delphi XE5
BeitragVerfasst: Mo 17.12.07 15:40 
Titel: Re: Verständnisfrage zur Verbindung ZQuery - Datasource - DB
user profile iconbflegel hat folgendes geschrieben:
Über eine ZConnection stelle ich eine Verbindung zu einer embedded Firebird DB her. Über die ZQuery1 (Property Active ist im OI "false") kann ich Tabellen erstellen und Werte in die DB schreiben, ohne die Property per Code auf "true" zu setzen.


Hi,
darf ich mal fragen wie du die Tabellen erstellst, bzw. Werte in die DB schreibst? Dann kann ich dir die Farge vielleicht beantworten.

MfG

Björn
bflegel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 193
Erhaltene Danke: 1

Win XP, Win 7, BS2000
D5
BeitragVerfasst: Mo 17.12.07 20:40 
Hallo Siluro,

ich verwende folgende Prozeduren:

Zum Lesen:

ausblenden 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:
<span class="inlineSyntax"></span>
procedure TForm1.DB_lesen(alles_lesen : boolean; datum_von, datum_bis : TDate);
// Abfrage für DBGrid füllen
// RadioButton1 = alle Datensätze lesen
// RadioButton2 = Nur Datensätze von-bis lesen

begin
  try
    ZROQuery1.Active := false;
    ZROQuery1.SQL.Clear;
    if alles_lesen then
      ZROQuery1.SQL.Add ('select * from gesundheit order by datum;')
    else
      begin
        ZROQuery1.SQL.Add ('select * from gesundheit where datum >= ');
        ZROQuery1.SQL.Add (QuotedStr(DateToStr(datum_von)));
        ZROQuery1.SQL.Add (' and datum <= ');
        ZROQuery1.SQL.Add (QuotedStr(DateToStr(datum_bis)));
        ZROQuery1.SQL.Add (' order by datum;');
      end;
    ZROQuery1.ExecSQL;
    ZROQuery1.Active := true;
  except
    ShowMessage('Fehler beim Lesen der DB');
  end;
end;


Zum Erstellen der DB:

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:
procedure TForm1.FormActivate(Sender: TObject);

begin
  if firststart then
    begin
      firststart := false;
      ZConnection1.User := 'sysdba';
      ZConnection1.Password := 'masterkey';
      ZConnection1.Protocol := 'firebird-1.5';
      ZConnection1.Database := db_file;
      ZConnection1.Properties.Add ('Dialect=3;');
      if not DirectoryExists (app_path + 'db\') then
        CreateDir (app_path + 'db\');
      if not FileExists (db_file) then
        begin
          Zconnection1.Properties.Add('CreateNewDatabase=CREATE DATABASE ' +
            QuotedStr (db_file) + ' USER ' +
            QuotedStr ('sysdba') + ' PASSWORD ' + QuotedStr ('masterkey') +
            ' PAGE_SIZE 4096 DEFAULT CHARACTER SET ISO8859_1');
          Zconnection1.Connect;
          /////////////////////
          ZQuery1.SQL.Clear;            // Active = false im OI
          ZQuery1.SQL.Add ('Create Table gesundheit ');
          ZQuery1.SQL.Add ('(datum date,');
          ZQuery1.SQL.Add (' uhrzeit time,');
          ZQuery1.SQL.Add (' syst smallint,');
          ZQuery1.SQL.Add (' diast smallint,');
          ZQuery1.SQL.Add (' puls smallint,');
          ZQuery1.SQL.Add (' gew decimal(4,1));');
          ZQuery1.ExecSQL;
        end;
    end;
  DB_lesen(RadioButton1.Checked,DateTimePicker1.Date,DateTimePicker2.Date);
end;


Zum Schreiben der Werte:

ausblenden 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:
procedure TForm1.Button_einfuegenClick(Sender: TObject);

begin
  Form1.Hide;
  if Form2.ShowModal = mrOK then
    begin
      try
        ZQuery1.Active := false;
        ZQuery1.SQL.Clear;
        ZQuery1.SQL.Add ('INSERT INTO gesundheit (datum,uhrzeit,syst,diast,puls,gew) VALUES ');
        ZQuery1.SQL.Add ('('+ QuotedStr(DateToStr(Form2.DateTimePicker1.Date)) + ',' +
                              QuotedStr(TimeToStr(Form2.DateTimePicker2.Time)) + ',' +
                              QuotedStr(Form2.Edit_syst.Text) + ',' +
                              QuotedStr(Form2.Edit_diast.Text) + ',' +
                              QuotedStr(Form2.Edit_puls.Text) + ',' +
                              QuotedStr(Form2.Edit_gewicht.Text) + ');');
        ZQuery1.ExecSQL;
      except
        ShowMessage ('Fehler beim Schreiben der' + #13#10 + 'Werte in die Datenbank!');
      end;
      DB_lesen(RadioButton1.Checked,DateTimePicker1.Date,DateTimePicker2.Date);
    end;
  Form1.Show;
  Form2.Close;
end;

_________________
I know all the jokes about my name
Siluro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 74

Win XP Prof., Win XP64 Prof., Win 7 Prof.
Delphi 7 Ent., Delphi XE, Delphi XE5
BeitragVerfasst: Di 18.12.07 11:08 
Hi bflegel,
so, oder so ähnlich hab ich mir das vorgestellt. Also, ExecSQL führt ein SQL-Script aus (z. B. Creates, Inserts, Updates, etc.) und schließt die Query nach dem Ausführen wieder.
Für Selects empfehle ich Open, Close und Refresh. Mit Open wird die Query auf den Status dsBrowse gesetzt und die Query bleibt geöffnet. Wenn die Query den Status dsBrowse hat kannst du ausserdem die Prozeduren Insert, Edit und Delete der Query nutzen, ist vielleicht ein bisschen einfacher als immer ein SQL-Script zu schreiben.
Die Eigenschaft Active benutze ich eigentlich nur um festzustellen ob die Query geöffnet ist.

Hoffe das hilft dir erstmal weiter. Wenn du noch fragen dazu hast, einfach posten.

MfG

Björn
bflegel Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 193
Erhaltene Danke: 1

Win XP, Win 7, BS2000
D5
BeitragVerfasst: Di 18.12.07 19:40 
Danke für die Ausführungen... ich werde mich mal durch die Begriffe ackern.
Wenn ich nicht weiterkomme, dann melde ich mich wieder.

bye
bflegel

_________________
I know all the jokes about my name