Mirror

Pack a Paradox or dBase table programatically (Views: 101)


Problem/Question/Abstract:

How to pack a Paradox or dBase table programmatically

Answer:

Solve 1:

function dgPackParadoxTable(Tbl: TTable; Db: TDatabase): DBIResult;
{Packs a Paradox table by calling the BDE DbiDoRestructure function. The TTable passed as the first parameter must be closed. The TDatabase passed as the second parameter must be connected.}
var
  TblDesc: CRTblDesc;
begin
  Result := DBIERR_NA;
  FillChar(TblDesc, SizeOf(CRTblDesc), 0);
  StrPCopy(TblDesc.szTblName, Tbl.TableName);
  TblDesc.bPack := True;
  Result := DbiDoRestructure(Db.Handle, 1, @TblDesc, nil, nil, nil, False);
end;


Solve 2:

If you use the DBGrid and DBNavigator to delete records in a table which has unique fields, you will find that the table grows relentlessly and you can't re-enter the same data without packing the table first.

The following routine will pack and reindex a DBase and Paradox table, taking from a few seconds to a few minutes. Tested with a 650K DBaseIV file. Much quicker after the first call.

Just add the code below to the relevant sections. Call the function with your table's name, then wait while it grinds away. Returns True if the table is packed successfully.

uses
  WinProcs, Classes, SysUtils, StdCtrls, Forms, Controls, DB, DBIProcs, DBITypes,
  DBIErrs, DBTables;

{Add to declarations}

function PackTable(tbl: TTable): Boolean;

{Add to Implementation}
  function PackTable(tbl: TTable): Boolean;

    {Packs a DBaseIV (and Paradox?) table}
  var
    crtd: CRTblDesc;
    db: TDataBase;
  begin
    try
      Screen.Cursor := crHourglass;
      Result := True;
      with tbl do
      begin
        db := DataBase;
        if Active then
          Active := False;
        if not db.Connected then
          db.Connected := True;
        FillChar(crtd, SizeOf(CRTblDesc), 0);
        StrPCopy(crtd.szTblName, TableName);
        crtd.bPack := True;
        if DbiDoRestructure(db.Handle, 1, @crtd, nil, nil, nil, False) <> DBIERR_NONE then
          Result := False;
        Open;
      end;
    except
      on Exception do {any exception}
        Result := False;
    end;
    Screen.Cursor := crDefault;
end;


Solve 3:

Wouldn't it be great for the TTable component to have a method that does this? Just open up a TTable, connected it to a table on disk, call the method, and wham! The table's packed (Hmm.... I just might have to look into that). But short of that, you have to make direct calls to the BDE to accomplish this task. For dBase tables, it's easy. There's a single function called dbiPackTable that'll pack any dBase file. For Paradox files, you have to jump through a couple of hoops first, then call dbiDoRestructure because Paradox tables can only be packed within the context of a table restructure (Note: If you've restructured a table in Paradox or the Database Desktop, you'll notice a checkbox at the bottom of the restructure dialog called "Pack Table").

Below is a simple procedure for packing tables. I took most of the code right out of the online help (yes, there's really good stuff in there if you know where look), and made some alterations. The difference between what I did and what the help file lists is that instead of the formal parameter being a TTable, I require a fully qualified file name (path/name). This allows for greater flexibility - the procedure opens up its own TTable and works on it instead you having to create one yourself. I guess it might all boil down to semantics, but I still like my way better (so there!). Check out the code below:

procedure PackTable(TblName: string);
var
  tbl: TTable;
  cProps: CURProps;
  hDb: hDBIDb;
  TblDesc: CRTblDesc;
begin

  tbl := TTable.Create(nil);
  with tbl do
  begin
    Active := False;
    DatabaseName := ExtractFilePath(TblName);
    TableName := ExtractFileName(TblName);
    Exclusive := True;
    Open;
  end;

  // Added 23/7/2000 to make sure that the current path is the same as the table
  //see note below

  SetCurrentDir(ExtractFilePath(TblName));

  // Make sure the table is open exclusively so we can get the db handle...
  if not tbl.Active then
    raise EDatabaseError.Create('Table must be opened to pack');

  if not tbl.Exclusive then
    raise EDatabaseError.Create('Table must be opened exclusively to pack');

  // Get the table properties to determine table type...
  Check(DbiGetCursorProps(tbl.Handle, cProps));

  // If the table is a Paradox table, you must call DbiDoRestructure...
  if (cProps.szTableType = szPARADOX) then
  begin
    // Blank out the structure...
    FillChar(TblDesc, sizeof(TblDesc), 0);
    // Get the database handle from the table's cursor handle...
    Check(DbiGetObjFromObj(hDBIObj(tbl.Handle), objDATABASE, hDBIObj(hDb)));
    // Put the table name in the table descriptor...
    StrPCopy(TblDesc.szTblName, tbl.TableName);
    // Put the table type in the table descriptor...
    StrPCopy(TblDesc.szTblType, cProps.szTableType);
    // Set the Pack option in the table descriptor to TRUE...
    TblDesc.bPack := True;
    // Close the table so the restructure can complete...
    tbl.Close;
    // Call DbiDoRestructure...
    Check(DbiDoRestructure(hDb, 1, @TblDesc, nil, nil, nil, False));
  end
  else
    {// If the table is a dBASE table, simply call DbiPackTable...} if
      (cProps.szTableType = szDBASE) then
      Check(DbiPackTable(tbl.DBHandle, tbl.Handle, nil, szDBASE, True))
    else
      // Pack only works on Paradox or dBASE; nothing else...
      raise EDatabaseError.Create('You can only pack Paradox or dBase tables!');

  with tbl do
  begin
    if Active then
      Close;
    Free;
  end;
end;

See? Nothing fancy. What you should know is that all operations involving dbiDoRestructure revolve around a table descriptor type CRTblDesc. With this record type, you can set various field values, then execute dbiDoRestructure to make your changes. That's kind of the trick to making BDE calls in general. You typically work with some sort of record structure, then use that structure in one of the calls. I know I'm probably oversimplifying, but that's it in a nutshell. The point? Don't be scared of the BDE. More later!

I encourage you to look at the BDE online help under any dbi- subject. There are lots of code examples that will get you on your way.

NOTE: I received an email from Stewart Nightingale who said "When running PackTable straight after saving something to floppy it became obvious that packing a table must use the current directory for temporary tables and things - the current directory was "a:" and it came up with an error saying "No Disk in Drive a:". I put in the line "SetCurrentDir(ExtractFilePath(TblName));" at the top of the procedure so it would work properly ." I have added this line to the code sample shown above - use it if you wish, leave it out if you do not...........

<< Back to main page