Delphi数据库的拼音检索(查询)方案Delphi数据库的拼音检索(查询)方案-汉字转换成拼音昨夜看到一篇文章,可以用Delphi将汉字->拼音,可是将拼音转换成汉字又该如何操作哪?我的数据库:通讯录软件缺少用拼音查找功能.结果,搞到12点还是无果!唉!中午找到输入拼音可以检索汉字,可是在数据库中怎样实现哪?2个小时,无果!今天实现:1.首先建立字库;2.检索:先从数据库读出所要查询字段所有记录,将其放入控件ListBox,再按照拼音检索需要的数据.实现是靠的迂回策略(读取数据库汉字->转换成拼音->依据拼音检索结果->实现数据库的拼音检索),没找到直接在数据库查询,然后显示结果的方法,希望高人指点一二。
下面附上Code:1.字库Code:{//汉字拼音码检索对应的拼音字母}function GetCharInd(zzchar:string):char;begincase 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';elseresult:=#0;end;end;2.查询实现部分:{汉字拼音码的检索}function DisByStrInd(ListBoxStr:TListBox;StrInd:string):string; label NotFound;varzzchar :string;i,j:integer;beginfor i:=0 to ListBoxStr.Items.Count-1 dobeginfor j:=1 to Length(StrInd) dobeginzzchar:=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:******************6.制作日期:2008.04.15 }{********************************************************************}unit SelectByPinYin;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, NEOFORM, ComCtrls, MenuBar, T oolWin, ExtCtrls, StdCtrls, DBCtrls,Buttons;typeTSelectPY = 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;varSelectPY: TSelectPY;getName:String;{函数在这里定义}function GetCharInd(zzchar:string):char;//汉字拼音码function DisByStrInd(ListBoxStr:TListBox;StrInd:string):string; implementationuses DataMain;{$R *.dfm}{//汉字拼音码检索对应的拼音字母}function GetCharInd(zzchar:string):char;begincase 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';elseresult:=#0;end;end;{汉字拼音码的检索}function DisByStrInd(ListBoxStr:TListBox;StrInd:string):string;label NotFound;varzzchar :string;i,j:integer;beginfor i:=0 to ListBoxStr.Items.Count-1 dobeginfor j:=1 to Length(StrInd) dobeginzzchar:=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;{在 FormCreate 中,将联系人姓名加入 ListBox1}procedure TSelectPY.FormCreate(Sender: TObject);vari:integer;begininherited;with adodm.PersonName dobeginlistBox1.Clear;//用循环的方法加入for i:=0 to adodm.PersonName.RecordCount-1 dobeginself.ListBox1.Items.Add(adodm.PersonName.FieldByName('姓名').AsString);adodm.PersonName.Next;end;listBox1.Sorted:=true;adodm.PersonName.First;//DateSet指针指向第一条记录end;// edit1.SetFocus;end;//实现单击选择性名procedure TSelectPY.ListBox2Click(Sender: TObject);var xIndex:integer;begininherited;xIndex:=self.ListBox2.ItemIndex;//得到Item选项的Indexlabel3.Caption:=self.ListBox2.Items.Strings[xIndex];//从Index 得到 Text;getName:=self.ListBox2.Items.Strings[xIndex];end;{输入拼音查找汉字}procedure TSelectPY.Edit1Change(Sender: TObject);varSelStr:string;begininherited;SelStr:='';ListBox2.Items.Text:=DisByStrInd(listBox1,Edit1.Text);end;{单击选择}procedure TSelectPY.ListBox1Click(Sender: TObject);var nIndex:integer;begininherited;nIndex:=ListBox1.ItemIndex;ListBox2.Items.Text:=ListBox1.Items.Strings[nIndex];getName:=self.ListBox2.Items.Text;end;end.。