我有一个表,其中包含两列代码,其中有所有的条形码和VARIABLE在开始时初始化为0。当我扫描条形码时,我必须更新表格,当我扫描两次VARIABLE = 2时,我必须添加1到变量。... 我试图做它,但它不起作用。任何人都可以帮助?更新表SQL
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());
}
}
});
ERROR:9月8日至3日:43:44.966 30393-30393/com.example.practicas.myapplication W/System.err的:Sintaxis incorrecta CERCA德拉言论报釜 'ON'。我试图改变查询字符串查询=“插入到表(代码,变量)值(?,0);” +“更新TABLE SET VARIABLE = VARIABLE + 1代码如何?”;我得到这个错误/System.err:executeQuery方法必须返回一个结果集。
如果发生错误,发布错误日志? – akhilesh0707
你试过VARIABLE + 1把()所以(VARIABLE + 1)吗? – cotnic
给[这个答案](https://stackoverflow.com/a/2718352/469080)一个去,我认为这是你需要的。基本上'INSERT或IGNORE'你的条形码,然后'更新'在随后的声明中的价值。 –