xbaseclipper

How to add a new field / column to an existing xBase file / table


I'm working on a legacy desktop application. It was written using Xbase++ from Alaska software. I'm just trying to add a new field to an existing db file but I can't find any documentation about how to do it.

I have looked at https://harbour.github.io/doc/ , http://www.ousob.com/ng/clguide/index.php , https://en.wikibooks.org/wiki/Clipper_Tutorial:_a_Guide_to_Open_Source_Clipper(s) , http://www.alaska-software.com/support/kbase-old.cxp without any luck. All that is documented is about creating a new db file from scratch. Is it even possible to modify a db file structure?

cFieldExist := .f.

FOR nField := 1 TO (oDbfMaster:ProType)->( FCount() )
    IF (oDbfMaster:ProType)->( FieldName( nField ) ) == 'newFieldName'
        cFieldExist  := .t.
    ENDIF   
NEXT

IF !cFieldExist  
    //Please help me here, I want to add a the new field 'newFieldName'
ENDIF

Solution

  • In the old days, using dBase or Clipper we used to open the table, copy the structure to a new table:

    USE dbFile COPY STRUCTURE EXTENDED TO tempFile

    In the new table each row is a field from the original table. You append a new field and fill in the field name, data type, field length, no. of decimals, etc.

    Then using the temp file, you create a new db file and append the records into it from your old db:

    CREATE newFile FROM tempFile USE newFile APPEND FROM dbFile

    Lastly, you need to rename the old file and then rename the new file to that name and recreate any indexes.