想要一段 模糊查询的代码 比如:  在一个表里有 姓名,性别,年龄 等字段 朱茵  女     33 黄贯中 男     41我现在要做的是,在DEPHI界面上 ,一个文本框中 输入  朱茵 的 声母拼音 zy ,就可以定位到 姓名为朱茵这个记录上, 输入 hgz  就定位到  姓名为 黄贯中的  记录上。就是 按  拼音的 开头字母 模糊查询。等ing..............

解决方案 »

  1.   

    在数据表中加入CPYM(拼音码)字段,在程序运行时将CPYM字段隐藏。
      

  2.   

    你这不是什么模糊查询,是按拼音码条件进行查询。
    所以在数据库设计的时候,要考虑拼音码字段。将中文转化为拼音码的代码,在CSDN中有,自己找一下就可以了。
      

  3.   

    找到了在数据库输入中,怎样使"中文名称"输入翻译成"简码"存入另一字段?以便以后查询使用?如:"工资处"译成"GZC" //这个函数拿去用(我刚写好,已测试通过)function GetHzPy(const AHzStr: string): string;constChinaCode: array[0..25, 0..1] of Integer = ((1601, 1636), (1637, 1832), (1833, 2077),(2078, 2273), (2274, 2301), (2302, 2432), (2433, 2593), (2594, 2786), (9999, 0000),(2787, 3105), (3106, 3211), (3212, 3471), (3472, 3634), (3635, 3722), (3723, 3729),(3730, 3857), (3858, 4026), (4027, 4085), (4086, 4389), (4390, 4557), (9999, 0000),(9999, 0000), (4558, 4683), (4684, 4924), (4925, 5248), (5249, 5589));vari, j, HzOrd: integer;Hz: string[2];begini := 1;while i <= Length(AHzStr) dobeginif (AHzStr[i] >= #160) and (AHzStr[i + 1] >= #160) thenbeginHzOrd := (Ord(AHzStr[i]) - 160) * 100 + Ord(AHzStr[i + 1]) - 160;for j := 0 to 25 dobeginif (HzOrd >= ChinaCode[j][0]) and (HzOrd <= ChinaCode[j][1]) thenbeginResult := Result + char(byte('A') + j);break;end;end;Inc(i);end else Result := Result + AHzStr[i];Inc(i);end;end;///////////////////////////////////////这个函数用户识别单独汉字的简码 字符串的简码函数请自行制作function GetPYIndexChar(hzchar:string):char;begincase WORD(hzchar[1]) shl 8 + WORD(hzchar[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 := char(0);end;end;
      

  4.   

    给个思路://用下面的函数取得姓名的每个汉字的首个字母
    function GetEn(CnString: string): string;
      function GetEnChar(cnchar: string): char;
      begin
        case Word(cnchar[1]) shl 8 + Word(cnchar[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..$D1B8: result := 'x';
          $D1B9..$D4D0: result := 'y';
          $D4D1..$D7F9: result := 'z';
        else
          result := char(0);
        end;
      end;
    var
      i, len: integer;
    begin
      CnString := StringReplace(CnString, ' ', '', [rfReplaceAll]);  //删除空格
      len := Length(CnString) - Length(WideString(CnString));        //汉字个数
      for i := 1 to len do
        result := result + GetEnChar(copy(CnString, 2*i-1, 2));
    end;
     
    //例子
    procedure TForm1.Button1Click(Sender: TObject);
    begin
       ShowMessage(GetEn('朱茵'));
    end;//然后可以建一张临时表姓名,性别,年龄, 开头字母
     朱茵  女     33     zy 黄贯中 男     41    hgz//最后模糊查询就OK了
    SELECT 姓名 FROM 临时表 WHERE 开头字母 like ...