Skip site navigation (1) Skip section navigation (2)

Small problem in contrib/dbase/dbf2pg.c

From: "Thomas Behr" <Thomas(dot)Behr(at)BNV-Bamberg(dot)De>
To: pgsql-bugs(at)postgresql(dot)org
Cc: thomas(dot)Behr(at)uvw(dot)uni-bayreuth(dot)de
Subject: Small problem in contrib/dbase/dbf2pg.c
Date: 2003-07-17 15:06:08
Message-ID: 3F16D780.5880.23B6DDB@localhost (view raw or flat)
Thread:
Lists: pgsql-bugs
Your name               : Thomas Behr
Your email address      : ------(dot)----(at)bnv-bamberg(dot)de
System Configuration
---------------------
  Architecture (example: Intel Pentium)         :   ALL
  Operating System (example: Linux 2.0.26 ELF)  :   ALL
  PostgreSQL version (example: PostgreSQL-7.3.3):   PostgreSQL-7.3.3
  Compiler used (example:  gcc 2.95.2)          :   WorkShop Compilers 5.0 98/12/15 C 5.0
Please enter a FULL description of your problem:
------------------------------------------------
  dbf2pg  -  Insert  xBase-style  .dbf-files  into  a PostgreSQL-table
  There is an option "-s oldname=newname", which changes the old field name of  
  the dbf-file to the newname in PostgeSQL. If the length of the new name is 0, 
  the field is skiped. If you want to skip the first field of the dbf-file,
  you get the wildest error-messages from the backend.
  dbf2pg load the dbf-file via "COPY tablename FROM STDIN". If you skip the
  first field, it is an \t to much in STDIN.
Please describe a way to repeat the problem.   Please try to provide a
concise reproducible example, if at all possible:
----------------------------------------------------------------------
  dbf2pg -s first_field_name=,other_fieldname=,reserved_fieldname=new_fieldname 
 -c -d testdb -t testtable -h dbhost -U testuser -F IBM437 -T ISO-8859-1 -vv 
test.dbf 

If you know how this problem might be fixed, list the solution below:
---------------------------------------------------------------------

    440     /* build line and submit */
    441     result = dbf_get_record(dbh, fields, i); 
    442     if (result == DBF_VALID)
    443     {           
    444       query[0] = '\0';  
    445       for (h = 0; h < dbh->db_nfields; h++)
    446       {                 
    447         if (!strlen(fields[h].db_name))
    448           continue;                     
    449 
    450         if (h != 0)   /* not for the first field! */    
    451           strcat(query, "\t");    /* COPY statement field
    452                          * separator */



A fix could be an counter j=0, which increments only, if a field is imported 
(IF (strlen(fields[h].db_name)> 0) j++. And only if j > 1 (if an other field is 
imported) the \t is printed.

...
int j;
...

 if (result == DBF_VALID)
    {
      query[0] = '\0';
      j = 0;
      for (h = 0; h < dbh->db_nfields; h++)
      {
        if (!strlen(fields[h].db_name))
        {
          continue;
        }
        else
        {
          j++;
        }

        if (j > 1)   /* not for the first field! */
          strcat(query, "\t");    /* COPY statement field
                         * separator */


An other small bug in the README:
-s start
              Specify  the  first record-number in the xBase-file
              we will insert.
should be
-e start
              Specify  the  first record-number in the xBase-file
              we will insert.


Responses

pgsql-bugs by date

Next:From: Admin GastonDate: 2003-07-17 16:36:45
Subject: Postgresql 7.3.2-3 and 7.3.3-4 freeze
Previous:From: Bruce MomjianDate: 2003-07-17 13:25:33
Subject: Re: pg_dump -t option doesn't take schema-qualified table

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group