【发布时间】: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