Recent

Author Topic: Lazarus, PostgreSQL, "Field not found" error  (Read 1508 times)

Ali KOCA

  • New Member
  • *
  • Posts: 12
Re: Lazarus, PostgreSQL, "Field not found" error
« Reply #15 on: August 01, 2019, 11:05:17 am »
Code: MySQL  [Select][+][-]
  1. select  kullanici_ismi as is_im
  2. FROM kullanicilar;
  3.  

And there is both "i" and "_".

I don't think it's about the Turkish locale. And I've never had such a problem except postgres.

altanbozoglu

  • New Member
  • *
  • Posts: 10
Re: Lazarus, PostgreSQL, "Field not found" error
« Reply #16 on: December 01, 2019, 06:33:31 pm »
Hi;
It's been a long time ago, but maybe this solution might work.
The problem is that lazarus and fpc treat unicode strings differently.
If you test with a simple console application, you will see that the queries are running. The problem is somewhat related to the cwstring unit.
In my opinion, the best thing to do is to run the database part as a dynamic link library. I made a very simple example below.

Library
Code: Pascal  [Select][+][-]
  1.  
  2. library mydatalink;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. uses
  7.    Classes, SysUtils, parentunit, ZDataset, ZConnection, db;
  8.  
  9. function ServerCnn():Boolean cdecl;
  10. Begin
  11.   lasterror := False;
  12.  try
  13.   SQLServerCnn := TZConnection.Create(nil);
  14.   SQLServerCnn.Protocol  := 'postgresql';
  15.   SQLServerCnn.Database  := 'yourdatabasename';
  16.   SQLServerCnn.User      := 'usernamame';
  17.   SQLServerCnn.Password  := 'password';
  18.   SQLServerCnn.HostName  := 'serverip';
  19.   SQLServerCnn.Connected := True;
  20.   Result := SQLServerCnn.Connected;
  21.   Except on E : Exception do
  22.   Begin
  23.     lasterror    := True;
  24.     ErrorClass   := E.ClassName;
  25.     ErrorMessage := E.Message;
  26.   end;
  27.  end;
  28. end;
  29.  
  30. function SQLrun( SQLText: String):TDataSet; cdecl;
  31. var
  32.  sql:TZQuery;
  33. Begin
  34.   lasterror := False;
  35.   try
  36.     sql := TZQuery.Create(SQLServerCnn);
  37.     sql.Connection := SQLServerCnn;
  38.     sql.SQL.Text := SQLText;
  39.     sql.Open;
  40.     Result := sql;
  41.     Except on E : Exception do
  42.     Begin
  43.       lasterror    := True;
  44.       ErrorClass   := E.ClassName;
  45.       ErrorMessage := E.Message;
  46.       Result := nil;
  47.     end;
  48.   end;
  49. end;
  50.  
  51. function ErrorExsist():Boolean; cdecl;
  52. Begin
  53.   Result := lasterror;
  54. end;
  55.  
  56. function GetErrorClass():String; cdecl;
  57. Begin
  58.   Result := ErrorClass;
  59. end;
  60.  
  61. function GetErrorMessage():String; cdecl;
  62. Begin
  63.   Result := ErrorMessage;
  64. end;
  65.  
  66.  
  67. exports
  68. ServerCnn, SQLrun, ErrorExsist, GetErrorClass, GetErrorMessage;
  69. end.
  70.  
  71. //--------------------------------------------------------------
  72.  
  73. unit parentunit;
  74.  
  75. {$mode objfpc}{$H+}
  76.  
  77. interface
  78.  
  79. uses
  80.   Classes, ZConnection;
  81.  
  82.  type
  83.  
  84.    { TParentObject }
  85.  
  86.    TParentObject = class(TComponent)
  87.    private
  88.    public
  89.      connected:Boolean;
  90.      constructor Create(AOwner: TComponent); override;
  91.    end;
  92.  
  93.  var
  94.    ParentObj:TParentObject;
  95.    lasterror:Boolean;
  96.    ErrorClass:String;
  97.    ErrorMessage:String;
  98.    SQLServerCnn:TZConnection;
  99.  
  100. implementation
  101.  
  102.  
  103.  
  104. { TParentObject }
  105.  
  106. constructor TParentObject.Create(AOwner: TComponent);
  107. begin
  108.    inherited;
  109. end;
  110.  
  111.  
  112. end.
  113.  


Note: After compiling the dynamic link library, remember to copy it to /usr/lib

Sample Application
Code: Pascal  [Select][+][-]
  1.  
  2.   //...
  3.   function ServerCnn():Boolean cdecl; external 'libmydatalink.so';
  4.   function SQLrun( SQLText: String):TDataSet; cdecl; external 'libmydatalink.so';
  5.   function ErrorExsist():Boolean; cdecl; external 'libmydatalink.so';
  6.   function GetErrorClass():String; cdecl; external 'libmydatalink.so';
  7.   function GetErrorMessage():String; cdecl; external 'libmydatalink.so';
  8.  
  9. var
  10.   Form1: TForm1;
  11.  
  12. implementation
  13.  
  14. {$R *.lfm}
  15.  
  16. { TForm1 }  
  17.  
  18. procedure TForm1.Button1Click(Sender: TObject);
  19. begin
  20.  if ServerCnn() then
  21.    ShowMessage('Connected');
  22. end;
  23.  
  24. procedure TForm1.Button2Click(Sender: TObject);
  25. begin
  26.   DataSource1.DataSet := SQLrun('select * from testtable');
  27.   if ErrorExsist() then
  28.   Memo1.Lines.Text := GetErrorMessage();
  29. end;
  30.        
  31.  

Cheers..

 

TinyPortal © 2005-2018