Solved

How do I use DbiDoRestructure to add field?

Posted on 1997-10-24
5
1,281 Views
Last Modified: 2009-07-29
The function DbiDoRestructure seems to be useful when adding and deleting fields to a table....
How do I do it. I need a syntax example....
If I should add the field "test" containing a max-value of 10 characters to a table.
Is it possible to delete it later on?

I use Delphi Developer 2.0 and the table is a paradox-table..

Thanx!!
Erik N
0
Comment
Question by:Erik N
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:jackb022197
ID: 1348794
There is a full example in the BDE API Help file Delphi\Common Files\BDE\BDE32.HLP.
0
 

Author Comment

by:Erik N
ID: 1348795
Sorry jackb, I just need a simple example. I have trouble overviewing the example in the help-file...
Thanks anyway!!!

Erik N
0
 

Author Comment

by:Erik N
ID: 1348796
Adjusted points to 80
0
 
LVL 1

Accepted Solution

by:
jackb022197 earned 80 total points
ID: 1348797
Here's a piece of code that adds one field to a specified table; you need to make some changes for your own purposes, but this is what you are looking for:

procedure AddField(hDatabase:hDbiDb; TableName:TFileName);
var
  hTableCursor:  hDbiCur;
  Properties:    CURProps;
  FieldDescs:    PFLDDescs;
  FieldOps:      PCROpTypes;
  TableDesc:     CRTblDesc;
  NewFieldCount: integer;
  i:             integer;
  ValidityDesc:  VCHKDesc;
  ValidityOp:    CROpType;
  DefaultServes: Word;
begin
    Check(DbiOpenTable(hDatabase, PChar(TableName), szParadox, nil, nil,
                       0, dbiReadOnly, dbiOpenShared, xltField, False, nil,
                       hTableCursor));
    Check(DbiGetCursorProps(hTableCursor, Properties));
    // To fetch the field information we must first have somewhere to put it.
    NewFieldCount := Properties.iFields + 1;     // we're adding 1 extra field
    GetMem(FieldDescs, NewFieldCount*sizeof(FLDDesc));  // descriptors
    GetMem(FieldOps, NewFieldCount*sizeof(CROpType));   // operators
    try
      // Fetch the existing field definitions.
      Check(DbiGetFieldDescs(hTableCursor, @FieldDescs[1]));
      // Fields #2..#N are pushed right to occupy positions #3..#N+1.
      Move(FieldDescs^[2], FieldDescs^[3], (Properties.iFields-1)*sizeof(FLDDesc));
      for i := 3 to NewFieldCount do
        begin
          FieldOps^[i] := crCOPY;        // These destination fields are copies
          FieldDescs^[i].iFldNum := i-1; // of the nominated source fields
        end;
      // In between is the new field.
      FieldOps^[i] := crADD;    // this field is new
      FillChar(FieldDescs^[2], sizeof(FLDDesc), #0);
      with FieldDescs^[2] do
        begin
          StrCopy(szName,'Dish');        // Field Name
          iFldType := fldZSTRING;        // Field Type
          iUnits1 := 60;                 // Field Size
        end;

      // That's all for the field modifications (FieldOps & FieldDescs).
      // Default field values are (strangely) given in the validity check
      // structures.  We provide one of those to implement a Serves default.
      FillChar(ValidityDesc, sizeof(VCHKDesc), #0);
      ValidityOp := crADD;    // adding a new "validity check" descriptor
      with ValidityDesc do
        begin
          iFldNum := 2;       // New field number
//          bRequired := True;  // This is how you'd make the field required
          bHasDefVal := True;
          // Validity check properties are all of generic DBIVCHK type,
          // which you can't cast to.  Instead you must carefully Move,
          // ensuring source size matches size of field's logical iFldType.
          DefaultServes := 6;
          Move(DefaultServes, aDefVal, sizeof(Word));
        end;

      // Fill out a table descriptor, referring to the structures we've made.
      FillChar(TableDesc, sizeof(CRTblDesc), #0);
      with TableDesc do
        begin
          StrCopy(szTblName, Properties.szName);
          StrCopy(szTblType, szParadox);
//          bPack := True;   // you can pack it when finished if you like
          // field structure...
          iFldCount := NewFieldCount;
          pecrFldOp := @FieldOps^[1];
          pfldDesc := @FieldDescs^[1];
          // validity check (default value)...
          iValChkCount := 1;
          pecrValChkOp := @ValidityOp;
          pvchkDesc := @ValidityDesc;
        end;
      Check(DbiCloseCursor(hTableCursor));

      // If you didn't want the default value to apply to existing data
      // you'd probably have to use DbiRegisterCallback.  (I guess.)
      Check(DbiDoRestructure(hDatabase, 1, @TableDesc, nil, nil,
                             nil,      // << add Problem table here if required
                             False));
    finally
      FreeMem(FieldOps);
      FreeMem(FieldDescs);
    end;
end;

0
 

Author Comment

by:Erik N
ID: 1348798
Thank you jackb for this....

Erik N
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now