Solved

How do I use DbiDoRestructure to add field?

Posted on 1997-10-24
5
1,263 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

18 Experts available now in Live!

Get 1:1 Help Now