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中的内容一条一条的写到数据库中,怎么个写法~!~~~~~!!

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    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.
      

  2.   

    不加begin..end的话for循环执行的是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;
    所以肯定是一条了。
      

  3.   

    listbox1.Items.Clear 给清空了,前两位说的对
      

  4.   

    为什么现在有加不进去了啊?
    出现"List index out of bounds(1)"这样的错误??why??????????
      

  5.   

    怎么没有人来回答啊??
    我以前也遇到过这种问题<List index out of bounds(1)>好几次
    请高手指教
      

  6.   

    溢出了
    比如你的listbox有3条记录,应该是读0  1 2,你读listbox1.items[3]就会出错
    你这里都listbox1.Items.Clear了,根本没有内容,再去执行
        adoquery1.SQL.Add('insert into wxh(usersname) values ("'+listbox1.Items[i]+'")');
        此时i是大于0的,当然会报错
      

  7.   

    我不知道你把listbox1的内容写到listbox2里是干什么,因为后面没用到,你的代码应该改为
    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.
      

  8.   

    procedure TForm1.Button1Click(Sender: TObject);
    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.
      

  9.   

    还是哪个问题  List index out of bounds(1)我是把listbox1中的所以内容显示到listbox2中,并一条一条的写入数据库.
      

  10.   

    procedure TForm1.Button1Click(Sender: TObject);
    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.
      

  11.   

    把第一个listbox1.Items.Clear 删掉