procedure TForm1.Button1Click(Sender: TObject);
var
_Table: array[0..2] of array of TStrings;
_Row: array[0..2] of Integer;
_TextFile: TextFile;
S: String;
Tag: Integer;
Idx: Integer;
begin
Tag := -1;
_Row[0] := 0;
_Row[1] := 0;
_Row[2] := 0;
AssignFile(_TextFile, 'C:\Test.txt');
Reset(_TextFile);
while not Eof(_TextFile) do
begin
ReadLn(_TextFile, S);
if S = 'BA' then Tag := 0;
if S = 'BB' then Tag := 1;
if S = 'BC' then Tag := 2;
if (S <> 'BA') and (S <> 'EA') and
(S <> 'BB') and (S <> 'EB') and
(S <> 'BC') and (S <> 'EC') then
begin
case Tag of
0:
begin
if S <> '' then
begin
SetLength(_Table[0], _Row[0]+1);
_Table[0,_Row[0]] := TStringList.Create;
_Table[0,_Row[0]].Delimiter := ',';
_Table[0,_Row[0]].DelimitedText := S;
Inc(_Row[0]);
end;
end;
1:
begin
if S <> '' then
begin
SetLength(_Table[1], _Row[1]+1);
_Table[1,_Row[1]] := TStringList.Create;
_Table[1,_Row[1]].Delimiter := ',';
_Table[1,_Row[1]].DelimitedText := S;
Inc(_Row[1]);
end;
end;
2:
begin
if S <> '' then
begin
SetLength(_Table[2], _Row[2]+1);
_Table[2,_Row[2]] := TStringList.Create;
_Table[2,_Row[2]].Delimiter := ',';
_Table[2,_Row[2]].DelimitedText := S;
Inc(_Row[2]);
end;
end;
end;
end;
end;
for Idx := 0 to High(_Table[0]) do
begin
//插入A表
//A.Fields[0].Value := _Table[0, Idx][0];
//A.Fields[1].Value := _Table[0, Idx][1];
//A.Fields[2].Value := _Table[0, Idx][2];
end;
for Idx := 0 to High(_Table[1]) do
begin
//插入B表
//B.Fields[0].Value := _Table[1, Idx][0];
//B.Fields[1].Value := _Table[1, Idx][1];
//B.Fields[2].Value := _Table[1, Idx][2];
end;
for Idx := 0 to High(_Table[1]) do
begin
//插入C表
//C.Fields[0].Value := _Table[2, Idx][0];
//C.Fields[1].Value := _Table[2, Idx][1];
//C.Fields[2].Value := _Table[2, Idx][2];
end;
end;
var
vStringList: TStringList;
vTableName: string;
S: string;
I: Integer;
begin
vStringList := TStringList.Create;
vStringList.Text := Memo1.Text; // 载入text
vTableName := '';
for I := 0 to vStringList.Count - 1 do
begin
S := vStringList[I];
S := StringReplace(S, ',', ',', [rfReplaceAll]); // 去掉全角字符
if (Pos(',', S) <= 0 ) then
begin
if (Copy(S, 1, 1) = 'B') then
vTableName := Trim(Copy(S, 2, MaxInt));
end else if vTableName <> '' then
begin
S := StringReplace(S, ' ', '', [rfReplaceAll]); // 去掉空格
S := StringReplace(S, ',', '","', [rfReplaceAll]);
ShowMessage(Format('Insert into %s("%s")', [vTableName, S])); // 测试用
//ADOQuery1.SQL.Text := Format('Insert into %s(%s)', [vTableName, vStringList[I]]);
//ADOQuery1.ExecSQL; //执行插入语句
end;
end;
vStringList.Free;
end;