procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to ListBox1.Items.Count-1 do
ListBox2.Items.Add(ListBox1.Items[i]);
listbox1.Items.Clear ;
adoquery1.sql.Clear ;
adoquery1.SQL.Add('insert into wxh(usersname) values ("'+listbox1.Items[i]+'")');
adoquery1.ExecSQL;
end;
end.为什么只能写入一条记录到数据库中啊??
我要它把listbox1中的内容一条一条的写到数据库中,怎么个写法~!~~~~~!!
var
i:integer;
begin
for i:=0 to ListBox1.Items.Count-1 do
ListBox2.Items.Add(ListBox1.Items[i]);
listbox1.Items.Clear ;
adoquery1.sql.Clear ;
adoquery1.SQL.Add('insert into wxh(usersname) values ("'+listbox1.Items[i]+'")');
adoquery1.ExecSQL;
end;
end.为什么只能写入一条记录到数据库中啊??
我要它把listbox1中的内容一条一条的写到数据库中,怎么个写法~!~~~~~!!
var
i:integer;
begin
for i:=0 to ListBox1.Items.Count-1 do
begin//此加begin
ListBox2.Items.Add(ListBox1.Items[i]);
listbox1.Items.Clear ;
adoquery1.sql.Clear ;
adoquery1.SQL.Add('insert into wxh(usersname) values ("'+listbox1.Items[i]+'")');
adoquery1.ExecSQL;
end;//此加end
end;
end.
adoquery1.sql.Clear ;
adoquery1.SQL.Add('insert into wxh(usersname) values ("'+listbox1.Items[i]+'")');
adoquery1.ExecSQL;
所以肯定是一条了。
出现"List index out of bounds(1)"这样的错误??why??????????
我以前也遇到过这种问题<List index out of bounds(1)>好几次
请高手指教
比如你的listbox有3条记录,应该是读0 1 2,你读listbox1.items[3]就会出错
你这里都listbox1.Items.Clear了,根本没有内容,再去执行
adoquery1.SQL.Add('insert into wxh(usersname) values ("'+listbox1.Items[i]+'")');
此时i是大于0的,当然会报错
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
adoquery1.sql.Clear;//在这里clear,不然在循环里每次都clear了,当然只有一条
for i:=0 to ListBox1.Items.Count-1 do
adoquery1.SQL.Add('insert into wxh(usersname) values ("'+listbox1.Items[i]+'")'); listbox1.Items.Clear ;
adoquery1.ExecSQL;
end;
end.
var
i:integer;
begin
for i:=0 to ListBox1.Items.Count-1 do
begin
ListBox2.Items.Add(ListBox1.Items[i]);
// listbox1.Items.Clear ; 你已经全部删除了
adoquery1.sql.Clear ;
adoquery1.SQL.Add('insert into wxh(usersname) values ("'+listbox1.Items[i]+'")');
adoquery1.ExecSQL;
end;
listbox1.Items.Clear ; //如果确实需要
end.
var
i:integer;
begin
for i:=0 to ListBox1.Items.Count-1 do
begin//此加begin
ListBox2.Items.Add(ListBox1.Items[i]);
//listbox1.Items.Clear ;//去掉这一句,此处清空了listbox1.Items
//所以出现"List index out of bounds(1)"这样的错误
adoquery1.sql.Clear ;
adoquery1.SQL.Add('insert into wxh(usersname) values ("'+listbox1.Items[i]+'")');
adoquery1.ExecSQL;
end;//此加end
end;
end.