[Lazarus] TPQConnection and float

Björn Lundin b.f.lundin at gmail.com
Mon Mar 21 22:48:09 CET 2016


Hi !
I'm new to Lazarus (since Saturday)
but in the 90:ies id wrote some Turbo Pascal for school and liked it.
However, the last 15 years, I've worked with a system written in Ada
so that is the language I'm fluid in.
But it is close to Pascal.

So - I started to write a form with a tree widget and a graph,
and started go get data from a database to fill the graph with,
depending on the selected node in the tree.
But the values are off with a factor 10000.

I wrote a console test program to reproduce it,
and in also gets the floats wrong with a factor 10000.

Where should I report this - if this mailing list is not the place?
But perhaps first of all - am I doing something wrong here ?
code below.

Besides this, I am really impressed with Lazarus,
as an IDE.

I've already ordered some books at Amazon.

output as follows:
C:\pg_test_float>test_float.exe
A: 1
B:  1.0000000000000000E+004
C:  2.2000000000000000E+004
round(B): 10000
round(C): 22000

(Win7 , Postgres 9.4, libpq.dll also 9.4, Lazarus 1.6)

program test_float;
uses
pqconnection,sqldb ;

function CreateConnection: TPQConnection;
begin
  result := TPQConnection.Create(nil);
  result.Hostname := 'some-server';
  result.DatabaseName := 'some-db';
  result.UserName := 'some-user';
  result.Password := 'some-pass;
end;

function CreateTransaction(pConnection: TPQConnection): TSQLTransaction;
begin
  result := TSQLTransaction.Create(pConnection);
  result.Database := pConnection;
end;

function CreateQuery(pTransaction: TSQLTransaction): TSQLQuery;
begin
  result := TSQLQuery.Create(pTransaction.Database);
  result.Database := pTransaction.Database;
  result.Transaction := pTransaction
end;

var
PQConn : TPQConnection;
T      : TSQLTransaction;
Q1, Q2, Q3 : TSQLQuery;

A : LongInt;
B,C : Double;

sSql : String;
begin
  PQConn := CreateConnection ;
  PQConn.Open;
  T := CreateTransaction(PQConn);
  T.StartTransaction;

  Q1 := CreateQuery(T) ;
  sSql := 'create table TEST ( ';
  sSql += 'A integer not null primary key, ';
  sSql += 'B numeric(8,3) not null , ';
  sSql += 'C numeric(15,2) not null ) ';

  Q1.SQL.Text := sSql;
  Q1.ExecSql;

  Q2 := CreateQuery(T) ;
  sSql := 'insert into TEST values (1, 1.0, 2.2)';
  Q2.SQL.Text := sSql;
  Q2.ExecSql;

  Q3 := CreateQuery(T) ;
  sSql := 'select * from TEST order by A';
  Q3.SQL.Text := sSql;
  Q3.Open;
  if not Q3.Eof then begin
    A := Q3.FieldByName('A').AsLongint;
    B := Q3.FieldByName('B').AsFloat;
    C := Q3.FieldByName('C').AsFloat;
    Writeln('A: ', A);
    Writeln('B: ', B);
    Writeln('C: ', C);
    Writeln('round(B): ', round(B));
    Writeln('round(C): ', round(C));

  end
  else
    writeln('Eos');

  Q3.Close;
  T.Rollback;
  Q1.Free;
  Q2.Free;
  Q3.Free;
  T.Free;
  PQConn.Close;
end.


-- 
/Björn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160321/e4f0af3b/attachment-0002.html>


More information about the Lazarus mailing list