自定义数据集时,指定的数据域与SQL server中读取的定义无法匹配:
with FieldDefs do begin
Clear;
with AddFieldDef do begin
Name := 'Sort';
DataType := ftInteger;
Required := false;
end;
with AddFieldDef do begin
Name := 'LDateTimestr';
DataType := ftstring;
Required := false;
size:=30;
end;
下面的XML文件是从SQL中读取后保存的:除了字符串、整数,其它的数据类型,完全无法匹配。目前采用全字符串设计数据表,要得到其它的数据类型,到SQL server中去折腾吧,D7是指望不上了。
<?xml version="1.0" standalone="yes"?>
<DATAPACKET Version="2.0">
<METADATA>
<FIELDS>
<FIELD attrname="Sort" fieldtype="i4"/>
<FIELD attrname="LDateTimestr" fieldtype="string" WIDTH="30"/>
<FIELD attrname="Latitude" fieldtype="fixed" DECIMALS="4" WIDTH="9"/>
<FIELD attrname="Longitude" fieldtype="fixed" DECIMALS="4" WIDTH="9"/>
<FIELD attrname="Intension" fieldtype="fixed" DECIMALS="1" WIDTH="9"/>
<FIELD attrname="Gradient" fieldtype="fixed" DECIMALS="1" WIDTH="9"/>
<FIELD attrname="Error" fieldtype="fixed" DECIMALS="1" WIDTH="9"/>
<FIELD attrname="LocationMode" fieldtype="string" WIDTH="20"/>
</FIELDS><PARAMS/>
</METADATA>
<ROWDATA>
<ROW Sort="0" LDateTimestr="2012-07-03 00:00:35.9903823"
Latitude="32.5653" Longitude="116.6661" Intension="-66.3"
Gradient="-20.2" Error="0.0" LocationMode="二站混合"/>
</ROWDATA>
</DATAPACKET>
一个蛋疼的想法浪费我n长时间:
感觉Tclientdataset挺强的,刚好我有一堆文本文件想转移到数据库中。定义了一个数据集FTXTCDS,然后定义其字段,让FTXTCDS获取到文本的各字段值。最后定义连接到数据库的数据集FRDCDS,打开表,最后来个
FRDCDS.AppendData(FTXTCDS.DATA, false);
FTXTCDS中的数据一下子就到了FRDCDS中去了。但是问题随之而生:
1,e := FRDCDS.ApplyUpdates(-1);//无论如何都无法更新到数据库。
2, FThunderDM.DSPthunder.ApplyUpdates(FRDCDS.data, -1, e);//只有这样才能更新到数据库,多别扭。。。。
3,以为完工了,结果处理30天数据正常,处理31+天出现"mismatch in datapacket"错误。看了下保存的XML文件,感觉大概是FTXTCDS存在CHange_Log,FRDCDS没有的原因。。。想要删除FTXTCDS的LOG,设置了其属性不让产生LOG。奇怪的是数据库中没有提交数据了。。。。计划泡汤了。
解决办法:还是老实点,删除FTXTCDS,直写数据到FRDCDS中。。。暂时正常了。。。。难到AppendData这么鸡肋?
最后还是失败了,数据插入时间太长了,2个月的数据要半小时,1年数据12小时以上。
改成数据直写。。。。。。
FfoundLst:=TstringList.create;
fileLst:=TstringList.create;
LineLst:=TstringList.create;
LineLst.Delimiter := ' ';
EnumFileInQueue(Pchar(Filepath), Daystr, FfoundLst);
if FfoundLst.Count > 0 then
begin
fileLst.LoadFromFile(FfoundLst.Strings[0]);
if fileLst.Count > 0 then
begin
for i := 0 to fileLst.Count - 1 do
begin
LineLst.Clear;
LineLst.DelimitedText := fileLst.Strings[i];
if linelst.count > 0 then
begin
if linelst.count = 9 then
begin
FThunder.Sort := strtoint(LineLst.strings[0]);
FThunder.LDateTime := LineLst.strings[1] + ' ' + LineLst.strings[2];
FThunder.Latitude := Copy(LineLst.strings[3], 6, length(LineLst.strings[3]) - 5);
FThunder.Longitude := Copy(LineLst.strings[4], 6, length(LineLst.strings[4]) - 5);
FThunder.Intension := Copy(LineLst.strings[5], 6, length(LineLst.strings[5]) - 5);
FThunder.Gradient := Copy(LineLst.strings[6], 6, length(LineLst.strings[6]) - 5);
FThunder.Error := Copy(LineLst.strings[7], 6, length(LineLst.strings[7]) - 5);
FThunder.LocationMode := Copy(LineLst.strings[8], 11, length(LineLst.strings[8]) - 10);
end
else
begin
FThunder.Sort := strtoint(LineLst.strings[0]);
FThunder.LDateTime := LineLst.strings[1] + ' ' + LineLst.strings[2];
FThunder.Latitude := Copy(LineLst.strings[3], 6, length(LineLst.strings[3]) - 5);
FThunder.Longitude := Copy(LineLst.strings[4], 6, length(LineLst.strings[4]) - 5);
FThunder.Intension := Copy(LineLst.strings[5], 6, length(LineLst.strings[5]) - 5);
FThunder.Gradient := Copy(LineLst.strings[6], 6, length(LineLst.strings[6]) - 5);
FThunder.Error := Copy(LineLst.strings[9], 6, length(LineLst.strings[9]) - 5);
FThunder.LocationMode := Copy(LineLst.strings[10], 11, length(LineLst.strings[10]) - 10); ;
end; //if linelst.count = 9 then
with FThunderDM.ADOQthunder do
begin
Close;
sql.Clear;
sql.Add('insert ' + TBName + ' ([Sort],[LDateTime],[Latitude]' +
',[Longitude],[Intension],[Gradient],[Error]' +
',[LocationMode])values(:Sort,:LDateTime,:Latitude' +
',:Longitude,:Intension,:Gradient,:Error' +
',:LocationMode)');
Parameters.ParamByName('Sort').Value := FThunder.Sort;
Parameters.ParamByName('LDateTime').Value := FThunder.LDateTime;
Parameters.ParamByName('Latitude').Value := FThunder.Latitude;
Parameters.ParamByName('Longitude').Value := FThunder.Longitude;
Parameters.ParamByName('Intension').Value := FThunder.Intension;
Parameters.ParamByName('Gradient').Value := FThunder.Gradient;
Parameters.ParamByName('Error').Value := FThunder.Error;
Parameters.ParamByName('LocationMode').Value := FThunder.LocationMode;
ExecSQL;
end;
{with fRDCDS do
begin
//DisableControls;
if not active then open;
append;
FieldByName('Sort').Value := FThunder.Sort;
FieldByName('LDateTime').Value := FThunder.LDateTime;
FieldByName('Latitude').Value := FThunder.Latitude;
FieldByName('Longitude').Value := FThunder.Longitude;
FieldByName('Intension').Value := FThunder.Intension;
FieldByName('Gradient').Value := FThunder.Gradient;
FieldByName('Error').Value := FThunder.Error;
FieldByName('LocationMode').Value := FThunder.LocationMode;
post;
//EnableControls;
end; //with fRDCDS do}
end; // if linelst.count>0 then
end; //for
end; //if fileLst.Count > 0 then
end //
else
Elst.Add(format('文件:%s:%s没有找到。', [Filepath, Daystr]));
//fRDCDS.post;
LineLst.free;
fileLst.free;
FfoundLst.free;