![]() |
Re: Quellcode mal bitte angucken
Zitat:
[Edit]Uff... bei mir passier ja das selbe. Muss ich mir mal ansehen |
Re: Quellcode mal bitte angucken
Zitat:
Die PLZ als Integer zu speichern ist zwar effektiv. Aber (es gibt ja immer ein aber). Wenn ich eine Postleitzahl eingebe die mir 0 beginnt stimmt natürlich die Anzeige nicht mehr und auch das Suchen würde nicht sauber funktionieren. Darum würde ich da lieber ein VarChar(5) verwenden. |
Re: Quellcode mal bitte angucken
Hallo sharky,
erklär mal bitte für alle Ahnungslosen was ein varchar(5) ist. Rainer |
Re: Quellcode mal bitte angucken
Zitat:
das ist ein Datenbankfeld das bis zu 5 Zeichen Text auf nehmen kann. |
Re: Quellcode mal bitte angucken
Danke,
hab ich nicht gewusst. Rainer |
Re: Quellcode mal bitte angucken
Zitat:
noch zwei Anmerkungen :stupid: a) Warum stellst Du die Verbindung zu mySQL-Server im OnPaint deiner Form her? Wäre es nicht besser das einmalig im OnCreate zu machen. b) Du verwendest eine Spalte deiner StringGrid zum "speichern" der ID jeder Adresse. Ich habe mir angewöhnt dafür die Eigenschaft Obejects zu missbrauchen. Diese hast Du dann nämlich auch bei TListBoxen TListViews und allem was ein TStrings Property hat. Im OnSelectCell deines StringGrids müsstest Du das so änder:
Delphi-Quellcode:
und in deinem FillStringGrid noch das:
ID := Integer(StringGrid1.Objects[0,ARow]);
// ID := StrToIntDef(StringGrid1.Cells[0, ARow], 0);
Delphi-Quellcode:
StringGrid1.Objects[0,i+1] := TObject (Kontakte[i].ID);
// StringGrid1.Cells[0, i + 1] := IntToStr(Kontakte[i].ID); |
Re: Quellcode mal bitte angucken
Zitat:
Zitat:
Delphi-Quellcode:
Dann kann man das für jede beliebige Datenbank einsetzten.
type
TKonatkte = array of String; TCols = array of String; GetKontakte(var Kontakte: TKonatkte; var Cols: TCols); FillStringGrid(SG: TStringGrid, Kontakte: TKontakte, Cols: TCols); begin SG.Cols := length(Ciols); for i := 0 to length(Cols) - 1 do begin SG.Cells[0, i] := Cols[i]; end; SG.Rows := length(Kontakte); for i := 0 to length(Kontakte) - 1 do begin ....; ....; end; end; Denn wenn ich jetzt so was mache:
SQL-Code:
dann stürzt mit alles ab. Da muss ich noch eine universelle Lösung finden
SELECT name, vorname FROM kontakte
|
Re: Quellcode mal bitte angucken
So, hier du Umsetzung:
Delphi-Quellcode:
Und ein Aufruf könnte so aussehen:
type
TRows = array of array of string; // [Cols, Rows] TCols = array of string; function ExecQuery(const query: string; var Cols: TCols; var Rows: TRows): Boolean; var MySQLRes : PMYSQL_RES; MySQLRow : PMYSQL_ROW; AffectedRows : Int64; ColCount : Cardinal; Field : PMYSQL_FIELD; i : Integer; j : Integer; ErrorCode : Integer; begin ErrorCode := mysql_select_db(Descriptor, DBNAME); if ErrorCode = 0 then begin ErrorCode := mysql_real_query(Descriptor, PChar(query), length(query)); if ErrorCode = 0 then begin MySQLRes := mysql_store_result(Descriptor); if Assigned(MySQLRes) then begin ColCount := mysql_num_fields(MySQLRes); SetLength(Cols, ColCount); for i := 0 to ColCount - 1 do begin Field := mysql_fetch_field_direct(MySQLRes, i); Cols[i] := Field.Name; end; AffectedRows := mysql_affected_rows(Descriptor); SetLength(Rows, ColCount, AffectedRows); for i := 0 to ColCount - 1 do begin for j := 0 to AffectedRows - 1 do begin MySQLRow := mysql_fetch_row(MySQLRes); Rows[i, j] := MySQLRow[i]; end; mysql_real_query(Descriptor, PChar(query), length(query)); MySQLRes := mysql_store_result(Descriptor); end; log(Format('Betroffene Zeile: %d', [mysql_affected_rows(Descriptor)])); mysql_free_result(MySQLRes); end end end; result := ErrorCode = 0; end; procedure FillGrid(SG: TStringGrid; Cols: TCols; Rows: TRows); var i, j : Integer; begin SG.ColCount := 0; SG.RowCount := 0; if Assigned(Rows) then begin SG.RowCount := length(Rows[0]); SG.ColCount := length(Cols); SG.FixedRows := 1; for i := 0 to length(Cols) - 1 do begin SG.Cols[i].Add(Cols[i]); SG.Cells[i, 0] := Cols[i]; end; for i := 0 to length(Cols) - 1 do begin for j := 0 to length(Rows[0]) - 1 do begin SG.Cells[i, j + 1] := Rows[i, j]; end; end; end; end;
Delphi-Quellcode:
if ExecQuery(query, Cols, Rows) then
FillGrid(StringGrid1, Cols, Rows) |
Re: Quellcode mal bitte angucken
375 Aufrufe! :shock: Liegt das jetzt an mir oder an dem Thema? :gruebel:
|
Re: Quellcode mal bitte angucken
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:14 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz