现将csv格式的.txt文件读入到数据库中。
例如:
152,6584,48623,asdf,jh4g,5465
jasdf,,sdj,651,54,df
dfdsa,,,,sdklfj,jkl
65454,56,13,2314,2346,每行的数据数一定的,用逗号分割的!如果数据为空,逗号却不省略的!将该组数据读入到对应的数据库表中!谁可以给出来个正确的,详细的有注释的例子!!
例如:
152,6584,48623,asdf,jh4g,5465
jasdf,,sdj,651,54,df
dfdsa,,,,sdklfj,jkl
65454,56,13,2314,2346,每行的数据数一定的,用逗号分割的!如果数据为空,逗号却不省略的!将该组数据读入到对应的数据库表中!谁可以给出来个正确的,详细的有注释的例子!!
哪位可以给出具体的拆分数据的思想?怎么拆,然后怎么存入数据库(Access)
var
myStrList:TStringList;
i,m,n:integer;
s,s1,s2,s3,s4:string;
begin
////以下是写入数据库代码
myStrList:=TStringList.Create;
Try
if not FileExists(MainDir+'Temp.txt') then
FileCreate(MainDir+'temp.txt');
myStrList.LoadFromFile(Maindir+'Temp.txt');
for i:=0 to myStrList.Count-1 do
begin
s:=myStrList.Strings[i];
s1:=copy(s,1,Pos(',',s)-1);
m:=Pos(',',s);
s:=copy(s,m+1,length(s)-m);
s2:=copy(s,1,Pos(',',s)-1);
m:=Pos(',',s);
s:=copy(s,m+1,length(s)-m); s3:=copy(s,1,Pos(',',s)-1);
m:=Pos(',',s);
s:=copy(s,m+1,length(s)-m);
s4:=s;
with DataModule1.ADOQPub do
begin
close;
sql.Clear;
sql.Add('INSERT INTO Consume(CardID,DT,ConsumeMoney,LeaveMoney) VALUES(:CardID,:DT,:ConsumeMoney,:LeaveMoney)');
//Parameters.ParamByName('DeviceID').Value:=;
Parameters.ParamByName('CardId').Value:=s1;
Parameters.ParamByName('DT').Value:=strToDateTime(s2);
Parameters.ParamByName('ConsumeMoney').Value:=strtoCurr(s3);
Parameters.ParamByName('LeaveMoney').Value:=strtoCurr(s4);
try
ExecSQL;
except
end;
end;
end;
finally
myStrList.Free;
end;
数据格式如下:
0001,000235,2003-12-12 10:10:12,100,100.12
0002,000236,2003-12-12 10:10:12,100,100.12
.........................
myFile:textfile;
i,j:integer;
line,detail:string;
begin
AsSignfile(myFile,openedfile);//openedfile是打开的文本文件
ReSet(myFile);
Readln(myFile,line);
detail:='';
while not Eof(myFile) do
begin
Readln(myFile,line);
Table1.Append;
for i:=1 to Length(line) do
begin
if line[i]=',' then
begin
在这里将detail的按字段顺序内容写入表中
detail:=''
else detail:=detail+line[i];
end;
Table1.Post;
Table1.Next;
end;
Closefile(myFile);
有点不一样,不过,两位写了这么多,辛苦了哈!各40分!