Delphi辅导:如何实现数据库的拼音查询
来源:优易学  2011-7-3 21:05:29   【优易学:中国教育考试门户网】   资料下载   IT书店

  1.首先建立字库;
  2.检索:先从数据库读出所要查询字段所有记录,将其放入控件ListBox,再按照拼音检索需要的数据.
  实现是靠的迂回策略(读取数据库汉字->转换成拼音->依据拼音检索结果->实现数据库的拼音检索),没找到直接在数据库查询,然后显示结果的方法,希望高人指点一二。
  下面附上Code:
  1.字库Code:
  {//汉字拼音码检索 对应的拼音字母}
  function GetCharInd(zzchar:string):char;
  begin
  case WORD(zzchar[1]) shl 8+WORD(zzchar[2]) of
  $B0A1..$B0C4:result:='A';
  $B0C5..$B2C0:result:='B';
  $B2C1..$B4ED:result:='C';
  $B4EE..$B6E9:result:='D';
  $B6EA..$B7A1:result:='E';
  $B7A2..$B8C0:result:='F';
  $B8C1..$B9FD:result:='G';
  $B9FE..$BBF6:result:='H';
  $BBF7..$BFA5:result:='J';
  $BFA6..$C0AB:result:='K';
  $C0AC..$C2E7:result:='L';
  $C2E8..$C4C2:result:='M';
  $C4C3..$C5B5:result:='N';
  $C5B6..$C5BD:result:='O';
  $C5BE..$C6D9:result:='P';
  $C6DA..$C8BA:result:='Q';
  $C8BB..$C8F5:result:='R';
  $C8F6..$CBF9:result:='S';
  $CBFA..$CDD9:result:='T';
  $CDDA..$CEF3:result:='W';
  $CEF4..$D188:result:='X';
  $D1B9..$D4D0:result:='Y';
  $D4D1..$D7F9:result:='Z';
  else
  result:=#0;
  end;
  end;
  2.查询实现部分:
  {汉字拼音码的检索}
  function DisByStrInd(ListBoxStr:TListBox;StrInd:string):string;
  label NotFound;
  var
  zzchar :string;
  i,j:integer;
  begin
  for i:=0 to ListBoxStr.Items.Count-1 do
  begin
  for j:=1 to Length(StrInd) do
  begin
  zzchar:=ListBoxStr.Items[i][2*j-1]+ListBoxStr.Items[i][2*j];
  if (StrInd[j]<>'?') and (UpperCase(StrInd[j])<>GetCharInd(zzchar))
  then goto NotFound;
  end;
  if result='' then result:=ListBoxStr.Items[i]
  else result:=result+#13+ListBoxStr.Items[i];
  NotFound:
  end;
  end;
  以下是Delphi7中 Unit单元 的完整代码:
  {********************************************************************}
  { *1.名称: SelectByPinYin 单元.
  *2.功能:本单元为此数据库程序的 通过汉字拼音查询 单元.
  *3.软件环境:Win98+Delphi7+AccessXp2002.
  *4.作者:Domain.
  *5.E-mail:dfkc6331@sina.com
  *6.制作日期:2008.04.15 }
  {********************************************************************}
  unit SelectByPinYin;
  interface
  uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, NEOFORM, ComCtrls, MenuBar, ToolWin, ExtCtrls, StdCtrls, DBCtrls,
  Buttons;
  type
  TSelectPY = class(TEDairyForm)
  Panel1: TPanel;
  Panel2: TPanel;
  Panel3: TPanel;
  Panel4: TPanel;
  ListBox2: TListBox;
  Edit1: TEdit;
  Label1: TLabel;
  Label2: TLabel;
  BitBtn1: TBitBtn;
  BitBtn2: TBitBtn;
  ListBox1: TListBox;
  Label3: TLabel;
  procedure FormCreate(Sender: TObject);
  procedure Edit1Change(Sender: TObject);
  procedure ListBox2Click(Sender: TObject);
  procedure ListBox1Click(Sender: TObject);
  private
  { Private declarations }
  public
  { Public declarations }
  end;
  var
  SelectPY: TSelectPY;
  getName:String;

[1] [2] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训