浏览器通过“get”方式查询数据URL样例:http://127.0.0.1:7777/query?sql=select * from t1
/query 表示是“查询”命令字
?后面是SQL参数
服务端代码:
procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
method, sql: string;
{$IFDEF ado}
dm: TfrmDMado;
{$ENDIF}
{$IFDEF odac}
dm: TfrmDModac;
{$ENDIF}
begin
method := ARequestInfo.Document; // 获取命令字
if method = ‘‘ then
Exit;
{$ifdef ado}
CoInitialize(nil);
{$endif}
try
if method = ‘/query‘ then // 查询数据
begin
sql := ARequestInfo.Params.Values[‘sql‘]; // 取SQL参数
if sql = ‘‘ then
Exit;
AResponseInfo.ResponseNo := 0;
AResponseInfo.ContentType := ‘text/html‘;
{$IFDEF ado}
dm := TfrmDMado.Create(nil);
{$ENDIF}
{$IFDEF odac}
dm := TfrmDModac.Create(nil);
{$ENDIF}
try
AResponseInfo.ContentText := dm.Query(sql); // 返回数据引擎查询到的JSON格式的数据
AResponseInfo.WriteContent; // 给申请者回复
finally
dm.Free;
end;
end;
except
on E: Exception do
begin
end;
end;
{$ifdef ado}
CoUninitialize;
{$endif}
end;