On Monday 01 October 2018 11:13:48 Mike Martin wrote:
> HI
> If I use printf to round a numeric value before inserting into postgres
> table it is altered to 1 rather than the value when it is put into a table

Hi! Function printf takes format string with parameters and then it
prints to the standard output. And returns 1 (true) if is succeed.

> example
> CREATE TABLE public.chksize
> (
>     size numeric(10,2), #does not matter what field type
>     path1 character varying COLLATE pg_catalog."default"
> )
> 
> this creates a value of 1 for every value
> 
> my $ins=$dbh->prepare("INSERT into chksize (size,path1) VALUES (?,?) ");
> open my $list ,'chksizer.txt';
> no strict 'refs';
> my @list=(<$list>);
> foreach my $k (@list){
> chomp $k;
> my ($size,$path)=split /,/,$k;
> my $size1=printf('%.1f',$size/(1024*1024));

... so value '%.1f',$size/(1024*1024) is printed to STDOUT. Printing
succeed and therefore into $size1 is assigned 1 (true value).

You probably want to use sprintf function which returns formatted string
instead of printing it to STDOUT.

> $ins->bind_param(1,$size1);
> $ins->bind_param(2,$path);
> $ins->execute;
> }
> 
> without printf this inserts proper value
> 
> my $ins=$dbh->prepare("INSERT into chksize (size,path1) VALUES (?,?) ");
> open my $list ,'chksizer.txt';
> no strict 'refs';
> my @list=(<$list>);
> foreach my $k (@list){
> chomp $k;
> my ($size,$path)=split /,/,$k;
> my $size1=$size/(1024*1024);
> $ins->bind_param(1,$size1);
> $ins->bind_param(2,$path);
> $ins->execute;
> }
> 
> Any ideas what is happening here?
> 
> thanks
> 
> Mike

Reply via email to