foxpro表的彻底删除问题(50分)

  • 主题发起人 主题发起人 小小豆豆龙
  • 开始时间 开始时间

小小豆豆龙

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将*.dbf表中的记录彻底删除,我用delete from xxx命令只能做标记,并不能彻底删除
请教如何在delphi中彻底删除*.dbf中的记录,用query控件。
 
使用SQL语句是没办法彻底删除的
必须使用BDE的函数DBIPACKDBF,就可以物理删除
你可以在这儿搜索DBIPACKDBF,有例子的,很详细
 
procedure PackTable(Table: TTable);
var
Props: CURProps;
hDb: hDBIDb;
TableDesc: CRTblDesc;
begin
// Make sure the table is open exclusively so we can get the db handle...
if not Table.Active then
raise EDatabaseError.Create('Table must be opened to pack');
if not Table.Exclusive then

raise EDatabaseError.Create('Table must be opened exclusively to pack');

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

// If the table is a Paradox table, you must call DbiDoRestructure...
if Props.szTableType = szPARADOX then begin
// Blank out the structure...
FillChar(TableDesc, sizeof(TableDesc), 0);
// Get the database handle from the table's cursor handle...

Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));
// Put the table name in the table descriptor...
StrPCopy(TableDesc.szTblName, Table.TableName);
// Put the table type in the table descriptor...
StrPCopy(TableDesc.szTblType, Props.szTableType);
// Set the Pack option in the table descriptor to TRUE...
TableDesc.bPack := True;
// Close the table so the restructure can complete...
Table.Close;
// Call DbiDoRestructure...

Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, False));
end
else
// If the table is a dBASE table, simply call DbiPackTable...
if (Props.szTableType = szDBASE) then
Check(DbiPackTable(Table.DBHandle, Table.Handle, nil, szDBASE, True))
else
// Pack only works on PAradox or dBASE; nothing else...
raise EDatabaseError.Create('Table must be either of Paradox or dBASE ' +

'type to pack');

Table.Open;

end;
 
SQL 是 结构 化查询 语言,

主要是 提供 对数据库的 建, 查,改 删 等 操作的吧.

对于一个数据库的 物理 DELETE ,

还是用 WinAPI 吧.
 
FORPRO的表要物理删除,必需要pack.具体可搜索一下以前的帖子,有很多的.
 
以上的代码必须引用BDE单元!
 
foxpro的表要物理删除,有zap,dele all, pack,blank很多的.
 
 implementation

   uses BDE;//做PACK必须引用次单元里的函数

   {$R *.DFM}

    procedure TfrmPack.FormCreate(Sender: TObject);

    var

     DBName:String;

    begin

    DBName:=ExtractFilePath(Application.ExeName);//得到数据库的位置

     {设置Table}

      tblDBASE.DatabaseName:=DBName;

      tblParadox.DatabaseName:=DBName;

      tblDBASE.TableName:='dbsTest.dbf';

      tblParadox.TableName:='pdxTest.db';

      tblDBASE.Active:=True;

      tblParadox.Active:=True;

      end;

     {物理删除数据库记录Pack a Paradox or dBASE table}

      // The table must be opened execlusively before calling this procedure...

      procedure TFrmPack.PackTable(FTable: TTable);

      var

       Props: CURProps;

        hDb: hDBIDb;

       TableDesc: CRTblDesc;

      begin

       FTable.Active := False;

       {当数据库打开失败时,这个循环语句能够让用户重试}

       repeat

       try

        FTable.Exclusive := True;

        FTable.Active := True;

        {如果正常打开数据库,则退出循环}

        Break;

        except

        on EDatabaseError do

        if Application.MessageBox(

         '以独占方式打开数据库时,出现错误---重试否?',

         '数据库错误',

         MB_OKCANCEL + MB_DEFBUTTON1) <> IDOK then

         Exit;

        end;

        until False;

       try

        {Check()用于校正和报告DBI底层错误;DbiGetCursorProps()用于取表光标属性}

        Check(DbiGetCursorProps(FTable.Handle, Props));// 获得表的属性已得到表的类型

        {如果是Paradox 表, 必须调用 DbiDoRestructure,重建数据库结构}

         if (Props.szTableType = szPARADOX) then

          begin

           FillChar(TableDesc, sizeof(TableDesc), 0);

           {从数据表光标获取数据库句柄}

          Check(DbiGetObjFromObj(hDBIObj(FTable.Handle), objDATABASE, hDBIObj(hDb)));

          {设置表的描述结构的Name/Type/bPack属性}

           StrPCopy(TableDesc.szTblName, FTable.TableName);

           StrPCopy(TableDesc.szTblType, Props.szTableType);

           TableDesc.bPack := True;

           {关闭表并调用api}

            FTable.Close;

            Application.ProcessMessages;

            Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, False));

            Application.ProcessMessages;

            FTable.Open;

            end

           { 如果是 dBASE 表, 只需要调用DbiPackTable...}

          else

           if (Props.szTableType = szDBASE) then

            begin

             Application.ProcessMessages;

             Check(DbiPackTable(FTable.DBHandle, FTable.Handle, nil, szDBASE, True));

             Application.ProcessMessages;

            end

          {不是dBase和Paradox表}

          else

           raise EDatabaseError.Create('数据库必须是 Paradox 或者 dBASE 类型,才能进行物理删除操作!!');

           finally

           FTable.Active := False;

           FTable.Exclusive := False;

           FTable.Active := True;

          end;

         end;

  procedure TfrmPack.BitBtndBaseClick(Sender: TObject);

   begin

    if OpenPictureDlg.Execute then

     DBImage1.Picture.LoadFromFile(OpenPictureDlg.FileName);

    end;

  procedure TfrmPack.BitBtnParadoxClick(Sender: TObject);

   begin

    if OpenPictureDlg.Execute then

     DBImage2.Picture.LoadFromFile(OpenPictureDlg.FileName);

   end;

  procedure TfrmPack.BitBtnPackdBASEClick(Sender: TObject);

   begin

    PackTable(tbldBASE);//物理删除dDBSE库

   end;

  procedure TfrmPack.BitBtnPackParadoxClick(Sender: TObject);

   begin

    PackTable(tblParadox);//物理删除Paradox库

  end;

 
老张:
匆匆粘上,自己慢慢看![:)]
 
接受答案了.
 
后退
顶部