如何在程序中对PARADOX数据库增加索引或删除原索引(均为主索引)?(50分)

  • 主题发起人 主题发起人 jw
  • 开始时间 开始时间
这是Delphi中创建一个索引(包括主索引)的例子:
The following example shows how to create a table.
if not Table1.Exists then
{do
n't overwrite an existing table }
begin
with Table1do
begin
Active := False;
{ The Table component must not be active }
{ First, describe the type of table and give it a name }
DatabaseName := 'DBDEMOS';
TableType := ttParadox;
TableName := 'CustInfo';
{ Next, describe the fields in the table }
with FieldDefsdo
begin
Clear;
Add('Field1', ftInteger, 0, True);
Add('Field2', ftString, 30, False);
end;

{ Next, describe any indexes }
with IndexDefsdo
begin
Clear;
{ The first index has no name because it is a Paradox primary key }
Add('', 'Field1', [ixPrimary, ixUnique]);
Add('Fld2Indx', 'Field2', [ixCaseInsensitive]);
end;

{ Now that we have specified what we want, create the table }
CreateTable;
end;
end;

要修改索引,可对Ttable的IndexDefs进行处理.
修改完后用IndexDefs.Update即可。
详细操作可查看TTable及TIndexDefs的帮助。
 
procedure fDbiAddIndex1(Tbl: TTable);
var
NewIndex: IDXDesc;
begin
if not Tbl.Exclusive then
raise EDatabaseError.Create('TTable.Exclusive must be set to ' +
'true in order to add an index to the table');
with NewIndexdo
begin
iIndexId:= 0;
bPrimary:= True;
bUnique:= True;
bDescending:= False;
bMaintained:= True;
bSubset:= False;
bExpIdx:= False;
iFldsInKey:= 1;
aiKeyFld[0]:= 1;
bCaseInsensitive:= False;
end;
Check(DbiAddIndex(Tbl.dbhandle, Tbl.handle, PChar(Tbl.TableName),
szParadox, NewIndex, nil));
end;

procedure fDbiDeleteIndex(Table: TTable;
Tag: Boolean);
var
ActiveIdx: IDXDesc;
begin
if not Table.Exclusive then
raise EDatabaseError.Create('Table must be opened exclusively to delete index');
Check(DbiGetIndexDesc(Table.Handle, 0, ActiveIdx));
// Cannot delete the active index, so change to default
Table.IndexName := '';
Table.IndexFieldNames := '';
Check(DbiDeleteIndex(Table.DBHandle, Table.Handle, nil, nil, ActiveIdx.szName,
ActiveIdx.szTagName, 0));
end;
 
用TQuery执行 Create Index 和 Drop Index 的sql命令
 
接受答案了.
 
后退
顶部