delphi mysql查询结果

时间:2018-04-17 05:45:10

标签: mysql delphi

使用mysql查询从数据库服务器读取数据,如下所示:

FDQuery1.SQL.Text := 'select * from `table` WHERE dcid=3;';
FDQuery1.Open;
memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );

但是当我必须使用循环来从mysql读取数据时

我需要一个数组来读取和存储数据

我在php中知道它就像这样

$arr = array();
while($obj = $q->fetch_object() ){
    $arr[] = $obj;
}

但是我怎样才能在Delphi中使用这种语法?

3 个答案:

答案 0 :(得分:1)

您不需要执行FDQuery1.First,因为它隐含在FDQuery1.Open中。但是,如果你想知道你有多少记录,那么做一个FDQuery1.Last很重要;在使用FDQuery1.RecordCount获取真实记录数之前,否则你可能会得到奇怪的结果。

您需要做的就是将数据放入备忘录中

FDQuery1.SQL.Text := 'select * from table WHERE dcid=3;';
memo1.Lines.clear;
FDQuery1.Open;
While not FDQuery1.Eof do
    begin
    memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );
    FDQuery1.next;  
    end;
end;
FDQuery1.Close;

尽管使用最少的异常处理的更好的解决方案是

FDQuery1.SQL.Text := 'select * from table WHERE dcid=3;';
memo1.Lines.clear;
try
try
   begin
     FDQuery1.Open;
    While not FDQuery1.Eof do
        begin
        memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );
        FDQuery1.next;  
        end;
    end;
     end;
except
 on E : Exception do
     begin
       showmessage ('Exception class name = '+E.ClassName+ slinebreak
                 +  'Exception message = '+E.Message);
      end  //on E
end; //try-except

finally
 FDQuery1.Close;
end; //try-finally

你提到了一个数组。如果您只想将数据放入备忘录中,但是 希望将数据放入数组(动态数据变量,如您在设计时不知道的那样),则不需要这样做您需要的许多记录元素,您需要多少个字段元素或每个字段的类型)然后您将使用以下代码。

(注意这是故意不优化的代码,因为我试图使过程清晰)

Const
  FirstRecordIndex = 0;
  FirstFieldIndex = 0;

Var 
  DataArray : Variant;
  TheRecordCount, TheFieldCount,
  RecordNumber, FieldNumber : integer;
  Data : variant;

begin
FDQuery1.SQL.Text := 'select * from table WHERE dcid=3;';
FDQuery1.Open;

FDQuery1.Last; //to get correct recordcount
TheRecordCount := FDQuery1.RecordCount;
TheFieldCount := FDQuery1.FieldCount;
FDQuery1.First; //go back to the beginning of the dataset

//set the dimensions of the 2D array of variants to hold data
DataArray := VarArrayCreate([FirstRecordIndex, TheRecordCount, FirstFieldIndex, TheFieldCount], varVariant ); //element can be of any type

//fill it
RecordNumber := -1;  //initialise record indexe to just before the start
While not FDQuery1.Eof do
        begin
        inc(RecordNumber); //point to next record element in the array
        for FieldNumber := FirstFieldIndex to TheFieldCount -1 do  //load all the fields of this record
           begin
           Data := FDQuery1.Fields[FieldNumber].asVariant; //get the data
           DataArray[RecordNumber, FieldNumber] := Data;  //put into array
           end;
        FDQuery1.next;  //get next record
        end; //while
    end;
    FDQuery1.Close;
end;

要重新获取数据,请使用

For RecordNumber := FirstRecordIndex  to  TheRecordCount -1 do
    For FieldNumber := FirstFieldIndex to TheFieldCount -1 do
            begin
            Data := DataArray[RecordNumber, FieldNumber] ;
           //do something with the data ie put into a memo
            end; 

答案 1 :(得分:0)

你可以使用这个,我希望帮助你:

FDQuery1.SQL.Text := 'select * from `table` WHERE dcid=3;';
FDQuery1.Open;
if FDQuery1.Active and (FDQuery1.RecordCount > 0) then
begin
    FDQuery1.First;
    While not FDQuery1.Eof do
    begin
        memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );
        FDQuery1.next;  
    end;
end;

答案 2 :(得分:0)

you can do this with array of array consept

for this first you must create your new type

type
  myArray = array [1..10] of integer;
  myData= array of myArray;

and then you must define your variable

var
  Records: myData;
  i:integer;

on,now we have aarray of array to save database records on it so

begin
FDQuery1.SQL.Text := 'select * from table WHERE type=3;';
FDQuery1.Open;
FDQuery1.First;

SetLength(Records ,FDQuery1.recordcount);//or maybe you need a do a select count(*) query first
i:=0;

While not FDQuery1.Eof do
  begin
        Records[i][1]:= FDQuery1.FieldByName('value1').AsString;
        Records[i][2]:= FDQuery1.FieldByName('value2').AsString;
        Records[i][3]:= FDQuery1.FieldByName('value3').AsString;
        Records[i][4]:= FDQuery1.FieldByName('value4').AsString;
        Records[i][5]:= FDQuery1.FieldByName('value5').AsString;
        Records[i][6]:= FDQuery1.FieldByName('value6').AsString;
    i:=i+1;
        FDQuery1.next;
  end;
    FDQuery1.Close;   
end;

now all of your selected database records are in your local variable you can go over the row and make new queries with FDQuery1...