Changing Database Structure 
Author Message
 Changing Database Structure

Hi all,

I've had, more than once, to change the structure (inside the .EXE program
itself) of some databases in an application I'm developing, but what I
normally do is add some extra fields. I usually copy the .DBF file to
another file (with an .OLD extension), for security purposes, read the
file's structure into an array, add the fields I desire (still on the
array), create an empty database with the structure contained on that array
and append all the information from the .OLD file. That is fine, but now I
want to **change the name of 2 fields**, not add or delete fields. If I read
the structure into an array and change it, when I append from the previous
file I'll loose the information on those two fields, because their names are
different. Obviously I could always read all the records (one by one)from
the old file and save them on the new file, but I'm trying to avoid that
situation. I'm thinking about reading the header of the .DBF file with the
FREAD() function, and writing directly the new name on top of it... Any
problems using this method? Can field names be longer that the previous
ones, that is, does the field name ocuppy a fixed length on the header or
does it have to be the same size (I haven't had the time to try reading the
header character by character yet). Does anyone have any information on how
the DBF header is layed out? Any other suggestions?

TIA.

Estv?o Raminhos



Fri, 06 Apr 2001 03:00:00 GMT  
 Changing Database Structure

Quote:

>Hi all,

>I've had, more than once, to change the structure (inside the .EXE program

Hi Estevao,
    1. create a structure from the old table ...

    local aOldStru := oldfile->(dbstruct())
    local cNewFile

    2. add the new fields to the end of the structure

    aadd(aOldStru,{"field1","C",50,0})
    aadd(aOldStru,{"field2","C",50,0})

    3.    create the new table
    cNewFile := "mynewfil"
    dbcreate((cNewFile),aOldStru)

    4.  then append records from old to new
        append from

    5. then replace all the new fields values in the file with their old
field equivalent values

    dbeval(?????)

        6. modify the structure of the new table to delete the now redundant
fields

                                                    Mark



Sat, 07 Apr 2001 03:00:00 GMT  
 Changing Database Structure


[on renaming fields]

Quote:
>     I'm thinking about reading the header of the .DBF file with the
> FREAD() function, and writing directly the new name on top of it... Any
> problems using this method?

None that I can think of.  DBU does it even.  Obviously the
DBF should not be in use, and you would not want to rename
fields whose names appear in any index expressions anywhere...

Quote:
>         Can field names be longer that the previous
> ones, that is, does the field name ocuppy a fixed length on the header or
> does it have to be the same size (I haven't had the time to try reading the
> header character by character yet). Does anyone have any information on how
> the DBF header is layed out? Any other suggestions?

The field names occupy exactly 11 bytes in the header (that
is 10 characters plus a NUL byte, chr(0).  If the names are
shorter than 10, the should be right-padded with NULs.  For
example a field named "SURNAME"  will be set up as with 4
trailing NULs.

As for the header layout, try looking at it a DBF with dos
DEBUG.  All header entries are 32 bytes long, the first
being a file header record and subsequent entries defining
the field structure (one for each field).  The list of
field info entries is terminated with an entry containing
a CR, chr(13) as the first byte.

This should be in your doc somewhere.  Or look for a file
called HELPPC21.ZIP on a ftp or web site - this contains
loads of useful into about most things DOS, but also some
common data structures (like DBFs...)
--
Pete
        "We have not inherited the earth from our ancestors,
         we have borrowed it from our descendants."



Sat, 07 Apr 2001 03:00:00 GMT  
 Changing Database Structure

Quote:

> > but now I want to **change the name of 2 fields**, not
> > add or delete fields... Obviously I could always read
> > all the records (one by one)from the old file and save
> > them on the new file, but I'm trying to avoid that
> > situation. I'm thinking about reading the header of the .DBF file with
the

> Do you know the commands SCATTER and GATHER? Make use
> of them and your copy routine will be very neat and
> simple.

I've never heard of these comands.  Are they Clipper 5.3?
Alan.


Mon, 09 Apr 2001 03:00:00 GMT  
 Changing Database Structure
[snip]

Quote:
> problems using this method? Can field names be longer that the previous
> ones, that is, does the field name ocuppy a fixed length on the header or
> does it have to be the same size (I haven't had the time to try reading the
> header character by character yet). Does anyone have any information on how
> the DBF header is layed out? Any other suggestions?

Fieldnames are fixed, 10 chars, null-padded; but for the
full story check out http://www.wotsit.org where I got
this information.

Richard



Mon, 09 Apr 2001 03:00:00 GMT  
 Changing Database Structure

Quote:

> Hi all,

> I've had, more than once, to change the structure (inside the .EXE program
> itself) of some databases in an application I'm developing, but what I
> normally do is add some extra fields. I usually copy the .DBF file to
> another file (with an .OLD extension), for security purposes, read the
> file's structure into an array, add the fields I desire (still on the
> array), create an empty database with the structure contained on that array
> and append all the information from the .OLD file. That is fine, but now I
> want to **change the name of 2 fields**, not add or delete fields. If I read
> the structure into an array and change it, when I append from the previous
> file I'll loose the information on those two fields, because their names are
> different. Obviously I could always read all the records (one by one)from
> the old file and save them on the new file, but I'm trying to avoid that
> situation. I'm thinking about reading the header of the .DBF file with the
> FREAD() function, and writing directly the new name on top of it... Any
> problems using this method? Can field names be longer that the previous
> ones, that is, does the field name ocuppy a fixed length on the header or
> does it have to be the same size (I haven't had the time to try reading the
> header character by character yet). Does anyone have any information on how
> the DBF header is layed out? Any other suggestions?

A second answer from me for this question: the Oasis is even
better than we think.
Check ftp://ftp.iag.net/pub/clipper/general/chgfield.zip
It purports to do what you want, though I haven't tested it.

Richard



Mon, 09 Apr 2001 03:00:00 GMT  
 Changing Database Structure
        Those commands are Fox.


says...

Quote:


>> > but now I want to **change the name of 2 fields**, not
>> > add or delete fields... Obviously I could always read
>> > all the records (one by one)from the old file and save
>> > them on the new file, but I'm trying to avoid that
>> > situation. I'm thinking about reading the header of the .DBF file with
>the

>> Do you know the commands SCATTER and GATHER? Make use
>> of them and your copy routine will be very neat and
>> simple.

>I've never heard of these comands.  Are they Clipper 5.3?
>Alan.

--

PC HotShots, Inc.
Baltimore, MD
410 426-7617
http://www.pchotshots.com
--> Let us bring out the *Power* of your PCs <--



Mon, 09 Apr 2001 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Store JPEGs in RB Database structure

2. Link To Goldmine - Variable Structure In Database

3. Clarion Database Structure

4. Access Databases without knowing File Structure

5. HELP : Look for information of database internal file structure

6. Wanted: A tool for viewing IDMS database structure.

7. Direct Access Database Record Structures

8. French language database structure

9. Need structure ideas for database record manipulation (tcl newbie)

10. Modify Structure and Change Name of fields

11. xBase structure changed by MS Excel

12. Losing data on data structure change

 

 
Powered by phpBB® Forum Software