Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#18

Re: Quellcode mal bitte angucken

  Alt 29. Jan 2005, 15:51
So, hier du Umsetzung:
Delphi-Quellcode:
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;
Und ein Aufruf könnte so aussehen:
Delphi-Quellcode:
if ExecQuery(query, Cols, Rows) then
  FillGrid(StringGrid1, Cols, Rows)
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat