0

我在奖励表中插入值时遇到问题。尝试解决它并找到其他帖子,但我找不到任何解决方案。

    String sql1 = "INSERT INTO rewards(project_id, 1st_reward,1st_description,                 2nd_reward, 2nd_description, 3rd_reward, 3rd_description, 4th_reward, 4th_description, 5th_reward, 5th_description)  "
                                        + "values('"
                                        + projectId
                                        + "','"
                                        + rwd1.getText().toString()
                                        + "','"
                                        + rwd1Desc.getText().toString()
                                        + "','"
                                        + rwd1.getText().toString()
                                        + "','"
                                        + rwd2.getText().toString()
                                        + "','"
                                        + rwd2Desc.getText().toString()
                                        + "','"
                                        + rwd3.getText().toString()
                                        + "','"
                                        + rwd3Desc.getText().toString()
                                        + "','"
                                        + rwd4.getText().toString()
                                        + "','"
                                        + rwd4Desc.getText().toString()
                                        + "','"
                                        + rwd5.getText().toString()
                                        + "','"
                                        + rwd5Desc.getText().toString()
                                        + "')"; 
stmt.executeUpdate(sql1);
4

3 回答 3

0

其他人在您的值中提到的重复列是rwd1.getText().toString()调用之一:

...
+ rwd1.getText().toString()
+ "','"
+ rwd1Desc.getText().toString()
+ "','"
+ rwd1.getText().toString()
...

另一种更干净地组合查询的方法是:

EntityManager em;

Query query = em.createQuery("INSERT INTO rewards(project_id,1st_reward,1st_description,2nd_reward,2nd_description,3rd_reward,3rd_description,4th_reward,4th_description,5th_reward,5th_description) values (:projectId,:rwd1,:rwd1Desc,:rwd2,:rwd2Desc,:rwd3,:rwd3Desc,:rwd4,:rwd4Desc,:rwd5,:rwd5Desc)");

query.setParameter("projectId", projectId);
query.setParameter("rwd1", rwd1.getText().toString());
query.setParameter("rwd1Desc", rwd1Desc.getText().toString());
query.setParameter("rwd2", rwd2.getText().toString());
query.setParameter("rwd2Desc", rwd2Desc.getText().toString());
query.setParameter("rwd3", rwd3.getText().toString());
query.setParameter("rwd3Desc", rwd3Desc.getText().toString());
query.setParameter("rwd4", rwd4.getText().toString());
query.setParameter("rwd4Desc", rwd4Desc.getText().toString());
query.setParameter("rwd5", rwd5.getText().toString());
query.setParameter("rwd5Desc", rwd5Desc.getText().toString());

query.executeUpdate();

以下是此方法的更多资源:hereherehere

于 2013-10-09T14:48:06.693 回答
0

您有 11 列和 12 个值...

你有一个额外的

+ "','"
+ rwd1.getText().toString()
+ "','"
于 2013-10-09T14:46:05.923 回答
0

您在 SQL 语句中提到了 11 列,但为其设置了 12 个值。这导致

列数与 mysql 数据库的 java 中第 1 行的值计数不匹配

更新您的查询以使用正确数量的列,然后使用相同数量和类型的值。

于 2013-10-09T14:45:40.933 回答