使用 DB2 自动增量获得异常
Getting Exception With DB2 Auto Increment
我创建了以下 table:
"CREATE TABLE ParsonCollection "
+ "(id integer not null GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
+ "name varchar(20),"
+ "eye varchar(20),"
+ "hair varchar(20),"
+ "height varchar(20),"
+ "weight varchar(20),"
+ "PRIMARY KEY (id))";
然后我试图插入 table,这就是我 运行 遇到问题的地方。当我尝试更改 "id" 列时,出现错误 "java.sql.SQLSyntaxErrorException: Attempt to modify an identity column 'ID'. " 这是插入语句的样子:
"insert into ParsonCollection values(" + q_surround(Name) + ","
+ q_surround(Eye) + "," + q_surround(Hair) + "," + q_surround(Height) + "," + q_surround(Weight) + ",1" + ")";
但是,当我删除插入到 "id" 中的字段时,出现以下错误:"java.sql.SQLSyntaxErrorException: The number of values assigned is not the same as the number of specified or implied columns." 这是此插入语句的样子:
"insert into ParsonCollection values(" + q_surround(Name) + ","
+ q_surround(Eye) + "," + q_surround(Hair) + "," + q_surround(Height) + "," + q_surround(Weight) + ")";
我该如何克服这个问题?似乎当我解决一个异常时,会弹出另一个异常,反之亦然。谢谢
您不能分配给标识列。由于无法传递所有值进行插入,因此需要枚举列(省略标识列):
"insert into ParsonCollection (
name,
eye,
hair,
height
weight
) values("
+ q_surround(Name)
+ "," + q_surround(Eye)
+ "," + q_surround(Hair)
+ "," + q_surround(Height)
+ "," + q_surround(Weight)
+ ")";
旁注:您的代码已打开 SQL 注入。您应该认真考虑使用准备好的语句和绑定参数,而不是连接查询字符串。
出于兴趣,另一种解决方案是将标识列设置为 IMPLICITLY HIDDEN
我创建了以下 table:
"CREATE TABLE ParsonCollection "
+ "(id integer not null GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
+ "name varchar(20),"
+ "eye varchar(20),"
+ "hair varchar(20),"
+ "height varchar(20),"
+ "weight varchar(20),"
+ "PRIMARY KEY (id))";
然后我试图插入 table,这就是我 运行 遇到问题的地方。当我尝试更改 "id" 列时,出现错误 "java.sql.SQLSyntaxErrorException: Attempt to modify an identity column 'ID'. " 这是插入语句的样子:
"insert into ParsonCollection values(" + q_surround(Name) + ","
+ q_surround(Eye) + "," + q_surround(Hair) + "," + q_surround(Height) + "," + q_surround(Weight) + ",1" + ")";
但是,当我删除插入到 "id" 中的字段时,出现以下错误:"java.sql.SQLSyntaxErrorException: The number of values assigned is not the same as the number of specified or implied columns." 这是此插入语句的样子:
"insert into ParsonCollection values(" + q_surround(Name) + ","
+ q_surround(Eye) + "," + q_surround(Hair) + "," + q_surround(Height) + "," + q_surround(Weight) + ")";
我该如何克服这个问题?似乎当我解决一个异常时,会弹出另一个异常,反之亦然。谢谢
您不能分配给标识列。由于无法传递所有值进行插入,因此需要枚举列(省略标识列):
"insert into ParsonCollection (
name,
eye,
hair,
height
weight
) values("
+ q_surround(Name)
+ "," + q_surround(Eye)
+ "," + q_surround(Hair)
+ "," + q_surround(Height)
+ "," + q_surround(Weight)
+ ")";
旁注:您的代码已打开 SQL 注入。您应该认真考虑使用准备好的语句和绑定参数,而不是连接查询字符串。
出于兴趣,另一种解决方案是将标识列设置为 IMPLICITLY HIDDEN