【问题标题】:Update table SQL更新表 SQL
【发布时间】:2017-08-03 08:48:27
【问题描述】:

我有一个包含两列 CODE 的表,其中所有的条形码和 VARIABLE 在开头都初始化为 0。我必须在扫描条形码时更新表格,当我扫描它两次时,我必须将 1 添加到变量中 VARIABLE =2 .... 我试过这样做,但它不起作用。有人可以帮忙吗?

 String query = "INSERT INTO TABLE (CODE,VARIABLE) VALUES(?,?) ON DUPLICATE KEY UPDATE VARIABLE='"+VARIABLE+1+"'";

                try {
                    if (connect != null) {
                           PreparedStatement statement = connect.prepareStatement(query);
                         statement.setString(1, "%" + res + "%");
                        statement.setInt(2,VARIABLE );

                        r=statement.executeQuery();
                        if (r.next()) {
                            message = "Updated";
                            String code = r.getString("CODE");
                            int var = r.getInt("VARIABLE");
                            INFOSOMME.setText(message);
                            INFOSOMME.setText(code);
                            INFOSOMME.setText(var);
                        } else {
                            message = "Error";
                            INFOSOMME.setText(message);
                        }
                    } else {
                        message = "Error in connection with SQL server";
                        INFOSOMME.setText(message);

                    }

                } catch (SQLException e) {
                    etat = false;
                    message = "Got an exception!";
                    System.err.println(e.getMessage());
                }


            }
        });

错误:08-03 09:43:44.966 30393-30393/com.example.practicas.myapplication W/System.err:Sintaxis wronga cerca de la palabra clave 'ON'。

我尝试将查询更改为 String query="INSERT INTO TABLE(CODE,VARIABLE) VALUES(?,0);" + "更新表集变量=变量+1 代码在哪里?";我得到了这个错误 /System.err: The executeQuery method must return a result set.

【问题讨论】:

  • 如果您遇到任何错误,请发布错误日志?
  • 你试过VARIABLE+1把( ) 所以(VARIABLE+1)放进去吗?
  • 试试this answer,我想这就是你需要的。基本上是INSERT OR IGNORE 你的条形码,然后UPDATE 后面的语句中的值。
  • 你为什么不使用 VARIABLE+1 作为参数,比如 VALUES(?,?) ?
  • 尝试使用 executeNonQuery() 代替

标签: java sql-server jdbc computer-science mysql-management


【解决方案1】:

尝试执行以下SQL命令:

String query = "INSERT INTO TABLE (CODE,VARIABLE) VALUES(?,?) ON DUPLICATE KEY UPDATE VARIABLE=(VARIABLE+1)";

【讨论】:

  • 感谢您的回答,但我仍然遇到同样的错误
  • 再次尝试输入“VARIABLE=(VARIABLE+1)”而不是“VARIABLE=(VALUES(VARIABLE)+1)”
【解决方案2】:

我已经发表了评论,但我想这里更好:

executeNonQuery

更新和插入不返回数据,因此您不能将 executeQuery 与它们一起使用(它会返回除受影响的行数之外的信息)

【讨论】:

  • @Mimi 有用吗?如果是,请将其标记为已接受的答案,如果不是,请告诉我们以进一步帮助您
  • 非常感谢您的回答。我更改了查询 String query = " Update TABLE SET VARIABLE=VARIABLE+1 WHERE CODE LIKE ?";它有效。没有更多错误
猜你喜欢
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 2015-10-01
  • 2018-04-18
  • 2016-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多