已解决
 
如何永久地删除数据库表中带有删除标记的记录
提问者:Nicholas   回答:2  浏览次数:720     悬赏分:0

 
|  评论(0) |

如何永久地删除数据库表中带有删除标记的记录 我用Delphi 3进行数据库编程时,希望能有一种简单且直接的方法永久地删除数据库表中带有删除标记的记录,就如使用Foxpro或Dbase中的PACK命令那样,而不希望要通过中间数据库过渡的形式,如 TABLE1.BatchMove方法),以节省宝贵的空间。

如果有了满意的回答请及时采纳,不要辜负了回答者!
回答
 
共 2 条
 
最佳答案
回答者:Tom(Tom)  级别:沙场军衔-中校 (2006-12-07 18:13:09)
  对于各种数据库类型,只有xBase和Paradox采用这种删除方式,为了释放空间你必须借助BDE API中。关于BDE API的详细介绍参见Delphi的BDE API Help。对于Paradox,使用DBIDoRestructure语句,而对于xBase采用DBIPackTable语句。下面是一个永久地删除Paradox数据库表中带有删除标记的记录的例子: 
    procedure TForm1.PackParadoxTable( TableName, Alias : String); 
    {TableName should be with extension - .DB} 
    var 
    hDB :hDBIDb; 
    hCursor :hDBICur; 
    DBResult :DBIResult; 
    PdoxStruct :CRTblDesc; 
    szTableName : PChar; 
    szAlias : PChar; 
    PathLength : Integer; 
    ErrMsg : String; 
    begin 
     PathLength := 120; 
     GetMem(szTableName, PathLength); 
     GetMem(szAlias, 20); 
     StrPCopy(szTableName, TableName); 
     StrPCopy(szAlias, Alias); 
     Try 
     {First you need to initialize the Borland Database Engine} 
     DBResult := DBIInit(nil); 
     if DBResult <> DBIERR_NONE then begin 
     MessageDlg('Error Initializing BDE',mtError,[mbOk],0); 
     DBIExit; 
     Exit; 
     end; 
     {If the Initializing was OK then Open the Database} 
     DBResult := DBIOpenDatabase(szAlias,'STANDARD',DBIREADONLY,DBIOPENSHARED,'',0,nil,nil,hDB); 
     if DBResult <> DBIERR_NONE then begin 
     case DBResult of 
     DBIERR_INVALIDFILENAME : ErrMsg := 'Invalid FileName'; 
     DBIERR_NOSUCHFILE : ErrMsg := 'No Such File'; 
     DBIERR_TABLEREADONLY : ErrMsg := 'Table is READ ONLY'; 
     DBIERR_NOTSUFFTABLERIGHTS : ErrMsg := 'Not Sufficent Table Rights'; 
     DBIERR_INVALIDHNDL : ErrMsg := 'Invalid Handle'; 
     DBIERR_LOCKED : ErrMsg := 'Table Locked'; 
     DBIERR_DIRBUSY : ErrMsg := 'Directory Busy'; 
     else 
     ErrMsg := 'Unknown Error'; 
     end; 
     MessageDlg(ErrMsg,mtError,[mbOk],0); 
     DBIExit; 
     Exit; 
     end; 
     {Opening the table returns a Handle to the table's cursor. So now Open the Table} 
     DBResult := DBIOpenTable(hDB,szTableName,'','','',0,DBIREADWRITE,DBIOPENEXCL, 
     xltNONE,False,nil,hCursor); 
     if DBResult <> DBIERR_NONE then begin 
     case DBResult of 
     DBIERR_INVALIDFILENAME : ErrMsg := 'Invalid FileName'; 
     DBIERR_NOSUCHFILE : ErrMsg := 'No Such File'; 
     DBIERR_TABLEREADONLY : ErrMsg := 'Table is READ ONLY'; 
     DBIERR_NOTSUFFTABLERIGHTS : ErrMsg := 'Not Sufficent Table Rights'; 
     DBIERR_INVALIDHNDL : ErrMsg := 'Invalid Handle'; 
     DBIERR_LOCKED : ErrMsg := 'Table Locked'; 
     DBIERR_DIRBUSY : ErrMsg := 'Directory Busy'; 
     DBIERR_OPENTBLLIMIT : ErrMsg := 'Open Tables Limit'; 
     DBIERR_INVALIDINDEXNAME : ErrMsg := 'Invalid Index Name'; 
     DBIERR_NOSUCHTABLE : ErrMsg := 'No Such Table'; 
     else 
     ErrMsg := 'Unknown Error'; 
     end; 
     MessageDlg(ErrMsg,mtError,[mbOk],0); 
     DBICloseDatabase(hDB); 
     DBIExit; 
     Exit; 
     end; 
     Try 
     DBICloseCursor(hCursor); {Table Cursor needs to be closed} 
     {Initialize the record Structure CRTblDesc} 
     FillChar(PdoxStruct, SizeOf(CRTblDesc),0); 
     StrPCopy(PdoxStruct.szTblName, TableName); 
     PdoxStruct.bPack := True; 
     DBResult := DBIDoRestructure(hDB,1,@PdoxStruct,nil,nil,nil,False); 
     if DBResult <> DBIERR_NONE then 
     MessageDlg('Failed to Pack Table',mtError,[mbOk],0); 
     Finally 
     DBICloseCursor(hCursor); 
     DBICloseDatabase(hDB); 
     DBIExit; 
     end; 
     Finally 
     FreeMem(szTableName, PathLength); 
     FreeMem(szAlias, 20); 
     end; 
    end; 
 
 
提问者对答案的评价:
 
 
    谢谢哈!!
 |  评论(0) |
回答者:Yin(Yin)  级别:- (2006-12-07 18:14:36)
同意!!!
 
 |  评论(0) |
提问者请及时处理问题
 
  公告区
  本周推荐专家
专家 专业分类
  Copyright © 2003-2005 Forlink Technologies All Rights Reserved 
版权所有 时力科技 联系:客户服务中心 京ICP证030637号