Z
zh_haoer
Unregistered / Unconfirmed
GUEST, unregistred user!
大家来看我代码,看看能不能优化下,执行数据库的操作,20000多条一下操作跟死机一样!<br>实际上说就因为2次循环操作数据库,所以运行起来,齐慢,(中间过程位生成“姓名编码”的过程),但top100的速度还可以。共25000条数据 ,不知道怎么优化,如下看我 的代码:<br>procedure TForm1.Button1Click(Sender: TObject);<br> function GetPYIndexChar( hzchar:string):char;<br> begin <br> case WORD(hzchar[1]) shl 8 + WORD(hzchar[2]) of <br> $B0A1..$B0C4 : result := 'A'; <br> $B0C5..$B2C0 : result := 'B';<br> $B2C1..$B4ED : result := 'C'; <br> $B4EE..$B6E9 : result := 'D'; <br> $B6EA..$B7A1 : result := 'E'; <br> $B7A2..$B8C0 : result := 'F'; <br> $B8C1..$B9FD : result := 'G'; <br> $B9FE..$BBF6 : result := 'H'; <br> $BBF7..$BFA5 : result := 'J'; <br> $BFA6..$C0AB : result := 'K'; <br> $C0AC..$C2E7 : result := 'L'; <br> $C2E8..$C4C2 : result := 'M'; <br> $C4C3..$C5B5 : result := 'N'; <br> $C5B6..$C5BD : result := 'O'; <br> $C5BE..$C6D9 : result := 'P'; <br> $C6DA..$C8BA : result := 'Q'; <br> $C8BB..$C8F5 : result := 'R'; <br> $C8F6..$CBF9 : result := 'S'; <br> $CBFA..$CDD9 : result := 'T'; <br> $CDDA..$CEF3 : result := 'W'; <br> $CEF4..$D188 : result := 'X'; <br> $D1B9..$D4D0 : result := 'Y'; <br> $D4D1..$D7F9 : result := 'Z'; <br> else <br> result := char(32); <br> end; <br> end; <br> <br> var <br> I: Integer;<br> PY: string; <br> s: string;<br> h1,h2: string;<br> begin<br> ADOConnection1.Connected:=true;<br> //ADOQuery1.Active:=true;<br> if not(ADOConnection1.InTransaction) then ADOConnection1.BeginTrans;<br> //ADOQuery1.Open;<br> // ADOQuery1.first;<br> // while not adoquery1.eof do<br> //begin<br> ADOQuery1.Close;<br> ADOQuery1.sql.Clear;<br> ADOQuery1.SQL.Text:=' select username from telephonebook where [114flag]=''2'' and (username<>null or username<>'''') ';<br> ADOQuery1.Open;<br> ADOQuery1.first;<br> while not adoquery1.eof do<br> begin<br> h1:=trim(ADOQuery1.fieldbyname('username').AsString);<br> edit1.Text:=h1;<br> // begin<br> s := '' ;<br> I := 1;<br> while I <= Length(h1) do<br> begin <br> PY := Copy(h1, I , 1);<br> if PY >= Chr(128) then <br> begin <br> Inc(I); <br> PY := PY + Copy(h1, I , 1);<br> s := s + GetPYIndexChar(PY); <br> end <br> else<br> s := s + PY; <br> Inc(I); <br> end;<br> h2 := s;<br> edit2.Text:=h2;<br> // end;<br><br> ADOQuery2.Close;<br> ADOQuery2.sql.Clear;<br> ADOQuery2.SQL.Text:=' update telephonebook set usercode='''+h2+''' where rtrim(telephonebook.username)='''+h1+''' and telephonebook.[114flag]=''2'' ';<br> ADOQuery2.ExecSQL;<br> ADOQuery1.next;<br> end;<br> //ADOConnection1.CommitTrans;<br> Application.MessageBox('新增用户姓名编码已经成功','提示',MB_ICONINFORMATION);<br> end;