Solved

How do I use DbiDoRestructure to add field?

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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