可以考虑先判断一下目标是否存在,存在不插入,或者改更新语句:c:=list.Count-1;
with ADOQuery1 do
begin
for I := 1 to c do
begin
txt:=list.Strings[i];
Close;
SQL.clear;
SQL.Text := 'SELECT COUNT(1) FROM 三年级 WHERE 题目文本 = ' + QuotedStr(txt);
Open;
if Fields[0].AsInteger > 0 then
//代表已经有数据,改更新
SQL.Text := 'UPDATE 三年级 SET 题目文本 = ' + QuotedStr(txt)
else
SQL.Add('insert into 三年级 (题目文本) values(' + QuotedStr(txt) + ')');
execsql;
end;
end;
with ADOQuery1 do
begin
for I := 1 to c do
begin
txt:=list.Strings[i];
Close;
SQL.clear;
SQL.Text := 'SELECT COUNT(1) FROM 三年级 WHERE 题目文本 = ' + QuotedStr(txt);
Open;
if Fields[0].AsInteger > 0 then
//代表已经有数据,改更新
SQL.Text := 'UPDATE 三年级 SET 题目文本 = ' + QuotedStr(txt)
else
SQL.Add('insert into 三年级 (题目文本) values(' + QuotedStr(txt) + ')');
execsql;
end;
end;
SQL.Add('insert into 三年级 (题目文本) values(a)');
改
SQL.Add('insert into 三年级 (题目文本) values(:a)');
c:=list.Count-1; with ADOQuery1 do
begin
close;
for I := 1 to c do begin
txt:=list.Strings[i];
SQL.clear;
SQL.Add('select 题目文本 from 三年级 where 题目文本=a');
parameters.ParamByName('a').Value:=txt;
open;
if RecordCount=0 then begin
SQL.clear;
SQL.Add('insert into 三年级 (题目文本) values(a)');
parameters.ParamByName('a').Value:=txt;
execsql;
end;
end;
end;
.Close;
.SQL.Cearl;
.SQL.Text或.SQL.Add();
.Open或.ExecSql;
上面是一个标准流程(虽然我不确认是不是最全的)