Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-03

Postgresql interface bronken for Raspberry pi model 1b

10 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Björn Lundin

Hi!
I'm using a pi as a monitor for some process, so I'm trying to get SDL2 and a working postgres interface on it.

However I'm running into trouble on timestamps fields on the pi.

I wrote a console program on Win7, using 32-bit compiler bundled with Lazarus 1.6. (I think is is fpc 3.0.0)
and that platform can connect to the db, and run the program.

C:\pascal\pg_test_float>test_float.exe
A: 1
B: 1.0000000000000000E+000
C: 4.2457016203831023E+004
round(B): 1
round(C): 42457

The same program on the pi crashes.

~ $ fpc -v
Free Pascal Compiler version 2.6.0-9+rpi1+wsf1 [2015/04/28] for arm Copyright (c) 1993-2011 by Florian Klaempfl and others

~ $ uname -a
Linux raspberrypi 4.1.7+ #817 PREEMPT Sat Sep 19 15:25:36 BST 2015 armv6l GNU/Linux

~/svn/bnlbot/botstart/bot-1-0/source/pascal $ ./test_float An unhandled exception occurred at $000302D4 :
EDatabaseError : : Unknown fieldtype for parameter "DT". $000302D4
$0001C4D0
$00020DE8
$00008760

~/svn/bnlbot/botstart/bot-1-0/source/pascal $ addr2line -e test_float 000302D4 0001C4D0 00020DE8 00008760
/home/pi/svn/bnlbot/botstart/bot-1-0/source/pascal/test_float.lpr:85 /home/pi/svn/bnlbot/botstart/bot-1-0/source/pascal/test_float.lpr:85 /home/pi/svn/bnlbot/botstart/bot-1-0/source/pascal/test_float.lpr:85 /home/pi/svn/bnlbot/botstart/bot-1-0/source/pascal/test_float.lpr:57

where line 57 is market <---- here in code below
ie. Q2.Params.ParamByName('DT').Value := now;
program test_float;
uses
pqconnection,sqldb,sysutils,db;
function CreateConnection: TPQConnection;
begin
  result := TPQConnection.Create(nil);
  result.Hostname := 'db.somewhwew.com';
  result.DatabaseName := 'asd';
  result.UserName := 'asd';
  result.Password := 'pwd';
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 : Double;
C : TDateTime;
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 timestamp(3) without time zone not null ) ';
  Q1.SQL.Text := sSql;
  Q1.ExecSql;
  Q2 := CreateQuery(T) ;
  sSql := 'insert into TEST values (1, 1.0, :DT)';
  Q2.SQL.Text := sSql;
  Q2.Prepare;
  //Q2.Params.ParamByName('DT').DataType := ftDateTime;
  Q2.Params.ParamByName('DT').Value := now;             <----Here
  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').AsDateTime;
      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.

Ideas ?

Björn

Reply by email

Michael Van Canneyt
to Björn Lundin
Björn Lundin wrote 5 lines
Hi! I'm using a pi as a monitor for some process, so I'm trying to get SDL2 and a working postgres interface on it. However I'm running into trouble on timestamps fields on the pi.

[snip]

Q2.Prepare; //Q2.Params.ParamByName('DT').DataType := ftDateTime; Q2.Params.ParamByName('DT').Value := now; <----Here Q2.ExecSql;

Don't use Value for parameters.

Always use .AsDateTime or whatever the actual type is supposed to be.

Michael.

Reply by email

Björn Lundin
to Michael Van Canneyt
Michael Van Canneyt wrote 18 lines
On Mon, 28 Mar 2016, Björn Lundin wrote: > However I'm running into trouble on timestamps fields on the pi. [snip] > Q2.Prepare; > //Q2.Params.ParamByName('DT').DataType := ftDateTime; > Q2.Params.ParamByName('DT').Value := now; <----Here > Q2.ExecSql; Don't use Value for parameters. Always use .AsDateTime or whatever the actual type is supposed to be. Michael.

Thanks for the reply.
Unfortuantly, it does not help.

Changing the line to

 Q2.Params.ParamByName('DT').AsDateTime := now;

works on win7/32 bit

C:\\pascal\pg_test_float>test_float.exe
A: 1
B: 1.0000000000000000E+000
C: 4.2457521073587959E+004
round(B): 1
round(C): 42458

but not on the pi.

~$ ./test_float
An unhandled exception occurred at $0003028C :
EDatabaseError : : Unknown fieldtype for parameter "DT". $0003028C
$0001C488
$00020DA0
$00008750

~$ addr2line -e ./test_float 0003028C 0001C488 00020DA0 00008750
/home/pi/svn/bnlbot/botstart/bot-1-0/source/pascal/test_float.lpr:85 /home/pi/svn/bnlbot/botstart/bot-1-0/source/pascal/test_float.lpr:85 /home/pi/svn/bnlbot/botstart/bot-1-0/source/pascal/test_float.lpr:85 /home/pi/svn/bnlbot/botstart/bot-1-0/source/pascal/test_float.lpr:57

where 57 is the line I changed

-- Björn

Reply by email

Michael Van Canneyt
to Björn Lundin
Björn Lundin wrote 26 lines
On 2016-03-28 10:27, Michael Van Canneyt wrote: > > > On Mon, 28 Mar 2016, Björn Lundin wrote: >> However I'm running into trouble on timestamps fields on the pi. > > [snip] > >> Q2.Prepare; >> //Q2.Params.ParamByName('DT').DataType := ftDateTime; >> Q2.Params.ParamByName('DT').Value := now; <----Here >> Q2.ExecSql; > > Don't use Value for parameters. > > Always use .AsDateTime or whatever the actual type is supposed to be. > > Michael. > Thanks for the reply. Unfortuantly, it does not help. Changing the line to Q2.Params.ParamByName('DT').AsDateTime := now;

I don't think the problem is in the SQLDB code, it is CPU-agnostic.

Michael.

Reply by email

Björn Lundin
to Michael Van Canneyt
Michael Van Canneyt wrote
I don't think the problem is in the SQLDB code, it is CPU-agnostic.

Hmm, ok.
I'm looking at the sqldb code, and I'll need some time to digest it. Meanwhile, I think I'll try using the PQConnection unit directly, to see if I get the same result there.

But it will most likely not be before next weekend.

Björn

Reply by email

Björn Lundin
to Michael Van Canneyt: I don't think the problem is in the SQLDB code, it is CPU-agnostic. Michael.
Michael Van Canneyt wrote
I don't think the problem is in the SQLDB code, it is CPU-agnostic.
Hmm, real strange
I chagne the code like this
 Q2 := CreateQuery(T) ;
 sSql := 'insert into TEST values (:INT, :FT, :DT)';
 Q2.SQL.Text := sSql;
 Q2.Prepare;
 Q2.Params.ParamByName('INT').AsInteger := 1;
 Q2.Params.ParamByName('FT').AsFloat := 2.3;
 Q2.Params.ParamByName('DT').AsString := '2016-03-23 12:12:12.123';
 Q2.ExecSql;

and I get

An unhandled exception occurred at $00030200 :
EDatabaseError : : Unknown fieldtype for parameter "INT".

that is, it has nothing to do with timestamps.
On the ame machine a pythön script is runnig, quering the same database, with no problem. So I _think_ the client installation of pg is ok.

~/svn/bnlbot/botstart/bot-1-0/source/pascal $ ls -la /usr/lib/libpq.so
lrwxrwxrwx 1 root root 12 May 31 2015 /usr/lib/libpq.so -> libpq.so.5.4

However, I'm not sure how to proceed.
Perhaps use fpc 3.0, that is download and compile it ?

I'll see if I find some intructions online for doing that.

-- Björn

Reply by email

Bo Berglund
to Björn Lundin
Björn Lundin wrote
That speaks for trying to compile 3.0.0. The pi is on wheezy, so there is no .deb package for it.

I made a script for installing FPV 3.0.0 and Lazarus 1.6 release on an RPi.
http://blog.boberglund.com/install_laz_pi.sh

You can have a look at it and strip off Lazarus if you like. It is not very difficult to build FPC from sources really... One caveat with wheezy and some Pi versions is that you should up swap to 1000 to not get caught in the linker.
Swap expansion is found in sudo raspi-config.

Bo Berglund Developer in Sweden

Reply by email

Björn Lundin
to Bo Berglund
Bo Berglund wrote 16 lines
On Mon, 28 Mar 2016 15:30:36 +0200, Björn Lundin wrote: > That speaks for trying to compile 3.0.0. > The pi is on wheezy, so there is no .deb package > for it. I made a script for installing FPV 3.0.0 and Lazarus 1.6 release on an RPi. http://blog.boberglund.com/install_laz_pi.sh You can have a look at it and strip off Lazarus if you like. It is not very difficult to build FPC from sources really... One caveat with wheezy and some Pi versions is that you should up swap to 1000 to not get caught in the linker. Swap expansion is found in sudo raspi-config.

Yes, I saw that link somewhere else.
I got 2 pi:s, one original B with 256 mb and one later B model with 512 mb RAM.

I downloaded and ran it 'as is'. The pi:s do have 1Bb each of swap, but both froze during compilation. Both are on rasbian wheezy.

I'll strip away lazarus as you say and try again.

If I don't get it to work, I'll look into cross compiling from win32 to the pi

Björn

Reply by email