program testDouble;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes,
  db, mysql51conn, sqldb,
  variants;


{$R *.res}

var
   Conn: TMySQL51Connection;
   Tran: TSQLTransaction;
   Q: TSQLQuery;
   d: double;

begin
  Conn:=TMySQL51Connection.Create(nil);
  Conn.HostName:='localhost';
  Conn.DatabaseName:='test';
  Conn.UserName:='root';
  Conn.Password:='root';

  Tran:=TSQLTransaction.Create(nil);
  Tran.DataBase:=Conn;

  Q:=TSQLQuery.Create(nil);
  Q.DataBase:=Conn;
  Q.Transaction:=Tran;

  Conn.Open;
  writeln('Connected');

  Conn.ExecuteDirect('CREATE TEMPORARY TABLE t (int_field INTEGER PRIMARY KEY, double_field double)');
  Conn.ExecuteDirect('INSERT INTO t VALUES(1, 40734.825668912039)');
  writeln('Table created');

  Q.SQL.Text:='select * from t';
  Q.Open;
  d:=40734.825668912039;
  Q.AppendRecord([2,d]);
  Q.ApplyUpdates;
  Q.Close;

  Q.SQL.Text:='select double_field, double_field-40734 from t';
  Q.Open;
  while not Q.Eof do begin
    writeln(Q.Fields[0].AsFloat:30:20, ':', Q.Fields[1].AsFloat:30:20);
    Q.Next;
  end;

  Tran.Commit;
  Conn.Close;

  Q.Free;
  Tran.Free;
  Conn.Free;

  writeln('End. Press any key');
  readln;
end.

