Solved

How do I use DbiDoRestructure to add field?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

719 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