這是以前用Delphi 寫的主要從ftp 下載每天台灣主要地區氣象
(W002.txt)並解析後寫入資料庫
procedure TForm1.Button3Click(Sender: TObject);
begin
{//ftpsv.cwb.gov.tw/pub/forecast/W002.txt }
if IdFTP1.Connected then IdFTP1.Disconnect; IdFTP1.Host :='ftpsv.cwb.gov.tw';
IdFTP1.Username := 'anonymous';
IdFTP1.Connect; IdFTP1.ChangeDir('pub/forecast/');
IdFTP1.Get('W002.txt','W002.txt',true);
IdFTP1.Disconnect;
end;
//======================================
TWeather = record
CityNo:integer;
CityName:string;
Weather_Hi:integer;
Weather_Lo:integer;
Disc:String;
fact:integer;
end;
//======================================
function SpiltString(iString:String):TWeather;
var strTmp1,str,str1,str2,strRetn:String;
i:integer; strList:TStringList;
retn: TWeather;
begin
strRetn:=StringReplace(iString,'*','',[rfReplaceAll] );
strRetn:=StringReplace(iString,' ','',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,' ',',',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'%',',',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'-',',',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'1','1',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'2','2',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'3','3',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'4','4',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'5','5',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'6','6',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'7','7',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'8','8',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'9','9',[rfReplaceAll] );
strRetn:=StringReplace(strRetn,'0','0',[rfReplaceAll] );
strTmp1:='';
for i:=1 to Length(strRetn) do begin
str := strRetn[i];
if str = ',' then begin
if str1 = '' then begin
str1 :=',';
strTmp1 := strTmp1 + str;
end;
end else begin
if str <> ' ' then begin
str1:='';
strTmp1 := strTmp1 + str;
end;
end;
end;
strList := TStringList.Create;
strList.Delimiter :=',';
strList.DelimitedText := strTmp1;
if strList.Count = 5 then begin
retn.CityName := strList[0];
retn.Disc := strList[1];
retn.fact :=StrToInt( trim( strList[2]));
retn.Weather_Hi := StrToInt( trim( strList[3]));
retn.Weather_Lo := StrToInt( trim( strList[4]));
end;
strList.Free;
strRetn :=strTmp1;
SpiltString := retn;
end;
end;
//==========================================
procedure TForm1.Button2Click(Sender: TObject);
var lstLIST:TStringList;
strTmp,strTmp1:string;
Temp:TWeather;
Sql:String;
i:integer;
begin
lstLIST:=TStringList.Create;
lstLIST.LoadFromFile(Edit1.Text);
if ADOConnection1.Connected then ADOConnection1.Close;
ADOConnection1.Open;
for i := 7 to 28 do begin
Temp.CityName :='';
strTmp:= lstLIST.Strings[i];
Temp := SpiltString(strTmp);
Sql := 'update Weather Set Weather_Hi ='+IntToStr(Temp.Weather_Hi)';
Sql:=Sql+',Weather_Lo ='+IntToStr(Temp.Weather_Lo)+',';
Sql:=Sql+' Disc ='''+Temp.Disc+''', fact = ' +intToStr(Temp.fact);
Sql:=Sql+' where CityName ='''+Temp.CityName+'''';
ADOConnection1.Execute(Sql);
end;
lstLIST.Free;
end;