jfperea :
I want to bring a field from a table and add 1 to that field, is it possible?
I tried this way, but the only thing it does is put 1 always.
try {
ps = cc.conectar.prepareStatement("UPDATE personeros SET votos = '" + 1 +
"' WHERE numero = '" + candidato + "'");
ps.executeUpdate();
} catch (Exception e) {
System.out.println("Error: " + e);
}
Tim Biegeleisen :
The syntax you want here is:
SET votos = votos + 1
Full code:
try {
ps = cc.conectar.prepareStatement("UPDATE personeros SET votos = votos + 1 WHERE numero = ?");
ps.setInt(1, candidato);
ps.executeUpdate();
}
catch (Exception e) {
System.out.println("Error: " + e);
}
As a side note, in my edit I am using a placeholder ?
to which I am binding the value candidato
. This is the whole point of using prepared statements, to avoid having to concatenate together literal fragments in your SQL string.
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=309965&siteId=1