pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pg_put_line() Frustration
4+ messages / 3 participants
[nested] [flat]

* pg_put_line() Frustration
@ 2005-06-20 13:29  Simon Hewitt <sejh79@hotmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Simon Hewitt @ 2005-06-20 13:29 UTC (permalink / raw)
  To: pgsql-php

Hi all,

I am currently working on an application that will input multple lines into 
a Postgre database. I am using the following code

  $sql = "COPY table_2 FROM STDIN DELIMITER AS '\t'";
  pg_query($dbc, $sql);
  $sql = $data1."\t".$data2."\t".implode("\t", 
$data)."\ttrue\t05-05-2005\n";
  pg_put_line($this->dbc, $sql);
  if(!pg_put_line($dbc, "\\N"))
  {
    pg_end_copy($dbc);
    $error_message('Could not enter details');
  }
  else
  {
     pg_end_copy($this->dbc);
     unset($data);
  }

This gives me an example line like

57	BELF	254	0	10877	5138	0	0	true	05-05-2005

To go into a table with fields INT, CHAR(4) INT, INT, INT, INT, INT, INT, 
BOOL, DATE

However, its not going in...

I'm getting an error

ERROR:  missing data for column "col2"

With no data being inserted.

If anyone could point out where I'm going wrong I'd be forever in their debt

Thanks

Simon





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: pg_put_line() Frustration
@ 2005-06-20 14:05  Michael Fuhr <mike@fuhr.org>
  parent: Simon Hewitt <sejh79@hotmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Michael Fuhr @ 2005-06-20 14:05 UTC (permalink / raw)
  To: simon.hewitt@greenhill-ymca.org; +Cc: pgsql-php

On Mon, Jun 20, 2005 at 01:29:47PM +0000, Simon Hewitt wrote:
> 
> I am currently working on an application that will input multple lines into 
> a Postgre database. I am using the following code

It's PostgreSQL or Postgres, not Postgre.

>  $sql = "COPY table_2 FROM STDIN DELIMITER AS '\t'";
>  pg_query($dbc, $sql);
>  $sql = $data1."\t".$data2."\t".implode("\t", 
> $data)."\ttrue\t05-05-2005\n";
>  pg_put_line($this->dbc, $sql);
>  if(!pg_put_line($dbc, "\\N"))

The last line above attempts to add a row with a single NULL value,
so you get an error about missing data for the other columns.  What's
the purpose of that line?  Why do you sometimes use $dbc and sometimes
$this->dbc?  Do they refer to the same connection or to different
connections?

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: pg_put_line() Frustration
@ 2005-06-20 14:36  Christopher Kings-Lynne <chriskl@familyhealth.com.au>
  parent: Simon Hewitt <sejh79@hotmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Christopher Kings-Lynne @ 2005-06-20 14:36 UTC (permalink / raw)
  To: simon.hewitt@greenhill-ymca.org; +Cc: pgsql-php

Use pg_copy_to and pg_copy_from instead...

Chris

Simon Hewitt wrote:
> Hi all,
> 
> I am currently working on an application that will input multple lines 
> into a Postgre database. I am using the following code
> 
>  $sql = "COPY table_2 FROM STDIN DELIMITER AS '\t'";
>  pg_query($dbc, $sql);
>  $sql = $data1."\t".$data2."\t".implode("\t", 
> $data)."\ttrue\t05-05-2005\n";
>  pg_put_line($this->dbc, $sql);
>  if(!pg_put_line($dbc, "\\N"))
>  {
>    pg_end_copy($dbc);
>    $error_message('Could not enter details');
>  }
>  else
>  {
>     pg_end_copy($this->dbc);
>     unset($data);
>  }
> 
> This gives me an example line like
> 
> 57    BELF    254    0    10877    5138    0    0    true    05-05-2005
> 
> To go into a table with fields INT, CHAR(4) INT, INT, INT, INT, INT, 
> INT, BOOL, DATE
> 
> However, its not going in...
> 
> I'm getting an error
> 
> ERROR:  missing data for column "col2"
> 
> With no data being inserted.
> 
> If anyone could point out where I'm going wrong I'd be forever in their 
> debt
> 
> Thanks
> 
> Simon
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
>               http://archives.postgresql.org



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* FW: pg_put_line() Frustration
@ 2005-06-20 15:21  Simon Hewitt <sejh79@hotmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Simon Hewitt @ 2005-06-20 15:21 UTC (permalink / raw)
  To: pgsql-php

>It's PostgreSQL or Postgres, not Postgre.

Apologies - combination of Monday and lazy fingers

>  if(!pg_put_line($dbc, "\\N"))

>The last line above attempts to add a row with a single NULL value, so you 
>get an error about missing data for the other columns.  What's the purpose 
>of that line?

:$ - The purpose of this line is to send the NULL value as laid out in the 
comments of pg_copy_from and not pg_put_line... Apologies - my mistake

The code is now working by correcting the NULL value. The code is now

  $sql = "COPY table_2 (f1, f2, f3, f4) FROM STDIN DELIMITER AS '\t'";
  pg_query($dbc, $sql);
  $sql = $data1."\t".$data2."\t".implode("\t", $data)."\n";
  pg_put_line($dbc, $sql);
  if(!pg_put_line($dbc, "\\N"))
  {
    pg_end_copy($dbc);
    $error_message('Could not enter details');
  }
  else
  {
     pg_end_copy($dbc);
     unset($data);
  }

In response to Chris Kings response, the reason that I am using put_line as 
opposed to copy_from is because I want to limit the fields that are being 
entered. In the example above I am now entering data only into 4 fields.

Apologies for abusing the lists time - promise to drink some caffeine before 
posting next time. Hope the example is at least semi useful to someone

Thanks again

Simon





^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2005-06-20 15:21 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-06-20 13:29 pg_put_line() Frustration Simon Hewitt <sejh79@hotmail.com>
2005-06-20 14:05 ` Michael Fuhr <mike@fuhr.org>
2005-06-20 14:36 ` Christopher Kings-Lynne <chriskl@familyhealth.com.au>
2005-06-20 15:21 FW: pg_put_line() Frustration Simon Hewitt <sejh79@hotmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox