Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I use DbiDoRestructure to add field?

Posted on 1997-10-24
5
Medium Priority
?
1,423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 320 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

604 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