Received: from servalys.hobby.nl (root@pm102.hobby.nl [212.19.199.67]) by hub.org (8.9.2/8.9.1) with ESMTP id JAA78518 for ; Sun, 11 Apr 1999 09:04:43 -0400 (EDT) (envelope-from wybo@servalys.hobby.nl) Received: (from wybo@localhost) by servalys.hobby.nl (8.8.8/8.8.8) id OAA07355; Sun, 11 Apr 1999 14:29:53 +0200 Date: Sun, 11 Apr 1999 14:29:53 +0200 Message-Id: <199904111229.OAA07355@servalys.hobby.nl> X-Authentication-Warning: servalys.hobby.nl: wybo set sender to wybo@servalys.hobby.nl using -f From: Wybo Dekker To: matthew@venux.net CC: pgsql-interfaces@postgreSQL.org In-reply-to: <4.1.19990410172612.009943e0@mail.venux.net> (message from Matthew Hagerty on Sat, 10 Apr 1999 17:36:29 -0400) Subject: Re: [INTERFACES] Fastest import? References: <4.1.19990410172612.009943e0@mail.venux.net> Matthew Hagerty wrote: > > Greetings, > > What is the fastest way to import data from an ASCII file? I have ~17,000 > records in a .DBF file that I need to get into my pg6.4.2 database. I have > written a program that reads the .DBF file, massages the data and writes it > out to a text file, one record per line. I do that straight under linux by reading the .dbf file with a small C-program which writes the data out in tab-separated records. Those can be read with the copy from command: /* __________ db2u __________ convert dbase file Usage: db2u < dbase-file.dbf > unix_database or db2u dbase-file.dbf > unix_database */ #include #include #include #define MAXFIELDS 100 /* maximum nr of fields */ void main(int argc, char *argv[]) { char *p,*q,*r; typedef char string11[12]; FILE *in; int i,j,n,field_pos[MAXFIELDS+1],nfield=0,pos,lrec; char *record,field_type[MAXFIELDS]; string11 field_name[MAXFIELDS]; struct { char version; /* dBase version */ char datum[3]; /* date of last update YYMMDD */ long nrec; /* nr of records in th database file */ short nhead; /* nr of bytes in the header */ short lrec; /* nr of bytes in the record */ char dum1[2]; char transact; /* flag for incomplete transaction */ char encrypt; /* encryption flag */ char lan[12]; /* reserved for network applications */ char mdx; /* flag for presence of mdx-file */ char dum2[3]; } header; struct { char name[11]; /* fieldname zero filled */ char type; /* field type (C, D, F, L, M, N) */ char dum1[4]; unsigned char length; /* field length in binary */ char ndec; /* field decimal count in binary */ char dum2[2]; char workid; /* work area ID */ char dum3[11]; } field; switch(argc) { case 1: in=stdin; break; case 2: if ((in = fopen(argv[1],"rb")) == NULL) { fprintf(stderr,"Input file %s not found\n",argv[1]); exit(1); } break; default: fprintf(stderr,"Usage: %s dbase_file > unix_database\n",argv[0]); fprintf(stderr,"Or: cat dbase_file | %s > unix_database\n",argv[0]); exit(1); } fread(&header,32,1,in); n=header.lrec; pos=1; while (n>1) { if (nfield==MAXFIELDS) { fprintf(stderr,"Too many fields\n"); exit(1); } fread(&field,32,1,in); strcpy(field_name[nfield],field.name); field_type[nfield]=field.type; field_pos[nfield++]=pos; pos+=(int)field.length; n-=(int)field.length; } for (i=0;i