Re: postgres copy

From: Vijay Deval <deval(at)giaspn01(dot)vsnl(dot)net(dot)in>
To: "samsom, debra" <dsamsom(at)bristol(dot)ca>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: postgres copy
Date: 2001-11-03 12:09:57
Message-ID: 3BE3DE95.3E973D04@giaspn01.vsnl.net.in
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

> "samsom, debra" wrote:
>
> Is there away to copy fixed length text files into an Postgresql
> table. After reading the doc's it looks Postgresql expects some sort
> of delimiter.

This is how I converted a flat file with fixed field lengths, that was
exported from dBASEIV. File has six fields. Third field is a date field.
Array flen is for field
length and nmchar for type. 0 is for numbers and 1 for text. I have
trimmed blanks from the data. If Postgresql data type is varchar, it
accepts variable length data. Constant flg is incremented if it keeps
reading blank spaces. If flg exceeds 2, program moves ahead without
putting out any char. Only change one needs to make in the script are in
the initial definition stages.

I named this script as bill.c after doing cc bill.c; a.out<bill.txt>tmp
gave me the file with "|" as FS. Choice of "|" takes care of text
containing a comma. array nmchar is 0 for numeric and 1 for char fields.

Vijay

#include <stdio.h>

#define FNM 6 /* number of fields */
#define F1 6 /* Length of first field */
main()
{
int c,i,len,count,flg;

int flen[] = {6,8,8,7,8,5};
int nmchr[] = {0,0,1,0,0,0};
flg=0;
count=0;
i=0;
len=F1;
c=getchar();
while (c != EOF){
if( (nmchr[i]==1)&&(count==0))
printf("'");
if(flg<2)
putchar(c);
else
;
count=count+1;
c=getchar();
if (c==' ')
flg=flg+1;
else
flg=0;
if ((count==len-1)&&(nmchr[i]==0)&&(c== ' '))
printf("0");

if( (count==len)&&(i<FNM-1)){
if( nmchr[i]==1)
printf("'|");
else
printf("|");
i=i+1;
len=len+flen[i];
if (nmchr[i]==1)
printf("'");

}
if(c=='\n'){
if (nmchr[i]==1)
printf("'");
count=0;
i=0;
len=F1;
putchar(c);
c=getchar();
}
}
return 0;
}

Vijay

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Josh Berkus 2001-11-04 01:53:02 Book reviews are up
Previous Message The Cadaver 2001-11-03 04:09:08