我想在数据库里新建5个表,表名分别为table1、table2、table3、table4、table5。下面是我写的程序,我在adoquery1里定义了一个参数ID,让for循环5次,每次都把i赋给ID,然后创建表,但是编译通不过procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
begin
adoquery1.close;
for i:=1 to 5 do
begin
adoquery1.sql.clear; adoquery1.sql.add('CREATE TABLE table'ID'(sno CHAR(5),sname CHAR(20));');
adoquery1.Parameters.ParamByName('ID').Value:=i;
adoquery1.ExecSQL;
end;
adoquery1.open;
end;
[Error] Unit1.pas(41): ')' expected but identifier 'ID' found
[Error] Unit1.pas(41): Statement expected, but expression of type 'String' found
[Error] Unit1.pas(45): Declaration expected but identifier 'adoquery1' found
[Error] Unit1.pas(46): '.' expected but ';' found
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
为什么阿?各位大侠帮帮忙!
var
i:Integer;
begin
adoquery1.close;
for i:=1 to 5 do
begin
adoquery1.sql.clear; adoquery1.sql.add('CREATE TABLE table'ID'(sno CHAR(5),sname CHAR(20));');
adoquery1.Parameters.ParamByName('ID').Value:=i;
adoquery1.ExecSQL;
end;
adoquery1.open;
end;
[Error] Unit1.pas(41): ')' expected but identifier 'ID' found
[Error] Unit1.pas(41): Statement expected, but expression of type 'String' found
[Error] Unit1.pas(45): Declaration expected but identifier 'adoquery1' found
[Error] Unit1.pas(46): '.' expected but ';' found
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
为什么阿?各位大侠帮帮忙!
你只能這樣做 adoquery1.sql.text := 'CREATE TABLE table' + IntToStr(i) + ' ...
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
begin
for i:=1 to 5 do
begin
adoquery1.sql.clear;
adoquery1.sql.add('CREATE TABLE table'+inttostr(i)+'(sno CHAR(5),sname CHAR(20));');
adoquery1.ExecSQL;
end;
end;
刚才执行程序时报错重复创建table,原来是因为我用了execsql又用open,呵呵,我真是菜