使用kbmmw smarthttpservice 簡單返回數據庫結果


這個很簡單,直接上碼。

服務器端聲明過程

[kbmMW_Rest('method:get, path:querytable')]
     [kbmMW_Method]
     function querytable(

          [kbmMW_Rest('value: "$tname", required: true')] const tblname:string;

          [kbmMW_Rest('value: "$id", required: true')] const id:string):Tkbmmemtable;

具體實現代碼

function TkbmMWCustomHTTPSmartService1.querytable(const tblname,
  id: string): Tkbmmemtable;
 var
   sqls:string;

begin
     if tblname='' then
      begin
          kbmMWRaiseHTTPError(500,'result:{"ok":"error","value":"table not exist! "}');
      end;

    sqls:='select * from '+tblname;
    if id<>'' then
     begin
       sqls:=sqls+' where CustNo='+id;

     end;

    cx.sql.text:=sqls;
     try
        cx.open;
     except
          on e:exception do

          kbmMWRaiseHTTPError(500 ,e.Message);

     end;

   result:=Tkbmmemtable(cx);

end;

運行結果

 

 由於這個返回對象是 Tkbmmemtable.

因此出現錯誤后,服務器返回的是500, 沒辦法顯示具體的錯誤。

其實對於這個問題,我們也可以解決。

首先我們增加一個 錯誤信息表

 

 然后我們把錯誤信息寫到這個表里面就可以了

function TkbmMWCustomHTTPSmartService1.querytable(const tblname,
  id: string): Tkbmmemtable;
 var
   sqls:string;

begin
result:=errtable;
   errtable.Active:=True;
   errtable.EmptyTable;
     if tblname='' then
      begin
          errtable.Append;
          errtable.FieldByName('ok').AsString:='error';
          errtable.FieldByName('msg').AsString:='table not exist!';
          errtable.Post;
         
         exit;

      end;

    sqls:='select * from '+tblname;
    if id<>'' then
     begin
       sqls:=sqls+' where CustNo='+id;

     end;

    cx.sql.text:=sqls;
     try
        cx.open;
     except
          on e:exception do
          begin
             errtable.Append;
             errtable.FieldByName('ok').AsString:='error';
             errtable.FieldByName('msg').AsString:=e.Message;
             errtable.Post;
             exit;

          end;

     end;

   result:=Tkbmmemtable(cx);

end;

我們訪問一下試試

沒問題了,一切消停了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM