Recent

Author Topic: [SOLVED]fcl-mustache, sqlite, httpserver issue.  (Read 852 times)

BSaidus

  • Hero Member
  • *****
  • Posts: 609
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
[SOLVED]fcl-mustache, sqlite, httpserver issue.
« on: May 26, 2024, 07:30:04 pm »
Hello.
Having an fphttpserver simple program running on windows. When responding to root (/) request, I read data from sqlite database & render html page using fcl-mustache package.
but results looks like ugly, the fields results precedes with questions marks (ex: a field value of ABCD will looks like ????ABCD).
here is a simple code.
Code: Pascal  [Select][+][-]
  1. procedure route3(aReq: TRequest; AResponse: TResponse);
  2.  
  3.   Function StringOrFile(S : String) : TMustacheString;
  4.   begin
  5.     With TFileStream.Create(S,fmOpenRead or fmShareDenyNone) do
  6.     try
  7.       SetLength(Result,Size);
  8.       ReadBuffer(Result[1],Size);
  9.     finally
  10.       Free;
  11.     end;
  12.   end;
  13.  
  14. var
  15.   lCnt: TSQLite3Connection;
  16.   ltr: TSQLTransaction;
  17.   lDs: TSQLQuery;
  18.  
  19.   lText: TMustacheString;
  20.   lTextRes: UTF8String;//TMustacheString;
  21.  
  22.   lM: TMustache;
  23.   lC: TMustacheDBContext;
  24.  
  25. begin
  26.   ltr := TSQLTransaction.Create(nil);   // transaction
  27.   lCnt := TSQLite3Connection.Create(nil);  // Connection
  28.   lCnt.Transaction := ltr;
  29.   lCnt.DatabaseName := 'gsystem.db';
  30.   lCnt.KeepConnection := True;
  31.   lCnt.Open();
  32.   lDs := TSQLQuery.Create(nil);   // Query
  33.   lDs.DataBase := lCnt;
  34.   lDs.SQL.Text := 'SELECT * FROM uc_system Where del=0';
  35.   lDS.Open();
  36.  
  37.  
  38.   lM := TMustache.Create(Nil);
  39.  
  40.   try
  41.  
  42.         lText := StringOrFile('app.tpl');
  43.  
  44.         lC := TMustacheDBContext.Create(Nil);
  45.         lC.AddDataset(lDs, 'sysparam');
  46.  
  47.         lM.Template := lText;
  48.         lTextRes := lM.Render(lC);
  49.        
  50.         AResponse.ContentType := 'text/html; charset=utf-8';
  51.         AResponse.ContentEncoding := 'UTF-8';
  52.         AResponse.Content := lTextRes;
  53.         AResponse.SendContent();
  54.  
  55.   finally
  56.     lM.Free;
  57.     lC.Free;
  58.     lDs.Free;
  59.     lCnt.Free;
  60.     ltr.Free;
  61.   end;
  62. end;      
  63.  

The strange thing, also, is that when using a simple GUI program (not web), It works perfectly.
I think, I miss something when it comes to serve a web page.

Thank you in advence.
« Last Edit: May 27, 2024, 10:13:52 am by BSaidus »
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

BSaidus

  • Hero Member
  • *****
  • Posts: 609
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
[SOLVED] Re: fcl-mustache, sqlite, httpserver issue.
« Reply #1 on: May 27, 2024, 10:13:03 am »
Hi,
Finally there is no problem. there is an issue in then encoding of fields in sqlite database.
This is strange since sqlite db is cross plateform.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

 

TinyPortal © 2005-2018