在Delphi中物理排序表 [英] Sorting a table physically in Delphi

查看:153
本文介绍了在Delphi中物理排序表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi似乎不喜欢多字段索引。

Delphi does not seem to like multi-field indexes.

如何对一个表进行物理排序,以便最终得到一个包含所需顺序行的表?

How do I physically sort a a table so that I wind up with a table that has the rows in the desired order?

示例:

mytable.dbf

mytable.dbf

Field   Field-Name   Field-Type   Size
  0     Payer        Character     35
  1     Payee        Character     35
  2     PayDate      Date
  3     Amount       Currency

我需要按照收款人+付款人的字母顺序生成一个表格

I need to produce a table sorted alphabetically by "Payee"+"Payer"

当我尝试使用时一个收款人+付款人的索引,我收到一个错误:

When I tried using an index of "Payee+Payer", I got an error:


字段索引超出范围

"Field Index out of range"


推荐答案

如果您仍在使用BDE,可以使用BDE API对DBF表进行物理排序:

If you're still using BDE you can use the BDE API to physically sort the DBF table:

uses
  DbiProcs, DbiTypes, DBIErrs;

procedure SortTable(Table: TTable; const FieldNums: array of Word; CaseInsensitive: Boolean = False; Descending: Boolean = False);
var
  DBHandle: hDBIDb;
  RecordCount: Integer;
  Order: SORTOrder;
begin
  if Length(FieldNums) = 0 then
    Exit;

  Table.Open;
  RecordCount := Table.RecordCount;
  if RecordCount = 0 then
    Exit;
  DBHandle := Table.DBHandle;
  Table.Close;

  if Descending then
    Order := sortDESCEND
  else
    Order := sortASCEND;

  Check(DbiSortTable(DBHandle, PAnsiChar(Table.TableName), nil, nil, nil, nil, nil,
    Length(FieldNums), @FieldNums[0], @CaseInsensitive, @Order, nil, False, nil, RecordCount));
end;

例如,在您的情况下:

  SortTable(Table1, [2, 1]); // sort by Payee, Payer

这篇关于在Delphi中物理排序表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆