I have a program that will update every one minute. Each time it updates, it will either add the time, date, demand, or it will update the demand according to the same time and date. However, my current code does not allow me to do that. I've tried add another code to rectify this problem, but it doesn't work. Does anyone know how to add a constraint so that when it encounters the same data and time, but different demand, it will just update the db with the new demand. Instead of adding a new row, which contains the same time and date but different demand.
My original code:
preparedStatement = connect
.prepareStatement("insert into testdb.emcsg values (?, ?, ? , ?, ?)");
preparedStatement.setInt(1, count+1);
preparedStatement.setString(2, d1);
preparedStatement.setString(3, d2);
preparedStatement.setString(4, d3);
preparedStatement.setString(5, d4);
preparedStatement.executeUpdate();
My edited code:
preparedStatement = connect
.prepareStatement("insert INTO testdb.emcsg values (?, ?, ? , ?, ?) ON DUPLICATE KEY UPDATE 5");
preparedStatement.setInt(1, count+1);
preparedStatement.setString(2, d1);
preparedStatement.setString(3, d2);
preparedStatement.setString(4, d3);
preparedStatement.setString(5, d4);
preparedStatement.executeUpdate();
This edited code came out with an error "You have an error in your SQL syntax;" Advice and help will be appreciated!