0

I have a table TotalSales on my database with columns Date(Primary) and Sales. I send query thru my Java Program.

I want to add a row if Date row not exists, and if Date row exists, the value on Sales will be updated. On update, the new value on Sales will be 'current value on Sales' + 'the value of variable totalBill'.

Lets say before execution: row under Sales = 0, after execution: row under Sales = Sales + totalBill;

I tried this code:

String query = "INSERT INTO TotalSales (Date, Sales) VALUES(date, totalBill)
                ON DUPLICATE KEY UPDATE Sales= VALUES(Sales)+VALUES(totalBill)";
st = con.prepareStatement(query);
st.execute();

But doesn't work:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'totalBill' in 'field list'

Can anyone help?

6
  • 1
    Do you have totalBill field in TotalSales table? Commented Oct 7, 2014 at 5:19
  • none, its a variable from the program. Commented Oct 7, 2014 at 5:24
  • in here you have query totalBill for table column name. Try to modify it and make it to data binding. Commented Oct 7, 2014 at 5:31
  • BTW don't call your column Date. It is a reserved word, and too generic. Use SaleDate or similar. Commented Oct 7, 2014 at 5:32
  • So, you are sending that string query to the database engine for execution. When it tries to excecute it, how will it know the value of totalBill? Only your program knows that. You need to use a parameterised statement. As I was typing an answer MadProgrammer put one in which is what I was going to say. Commented Oct 7, 2014 at 5:33

2 Answers 2

3

You need to take advantage of the parameterised nature of PreparedStatements and bind the values you want to apply before you execute the statement, something like...

String query = "INSERT INTO TotalSales (Date, Sales) VALUES(?, ?)
                    ON DUPLICATE KEY UPDATE Sales= VALUES(Sales)+?";
st = con.prepareStatement(query);
st.setDate(1, date);
st.setLong(2, totalBill);
st.setLong(3, totalBill);

for example

Take a look at Using Prepared Statements for more details

Sign up to request clarification or add additional context in comments.

3 Comments

Note that st needs to be a PreparedStatement.
@Turophile Well, I assumed that preapreStatement was assigning to a PreparedStatement, which was from the OP's code, but fair point
Great! My data were all string though st.setString() and instead of Sales = VALUES(Sales) + ? , its Sales = Sales+?
1

You are not appending the variable to your insert query, rather simply using a string. So change this:

String query = "INSERT INTO TotalSales (Date, Sales) VALUES(date, totalBill)
                ON DUPLICATE KEY UPDATE Sales= VALUES(Sales)+VALUES(totalBill)";

to

String query = "INSERT INTO TotalSales (Date, Sales) VALUES(" + date + "," + totalBill + ")
                ON DUPLICATE KEY UPDATE Sales= VALUES(Sales)+VALUES(totalBill)";

ADVICE: But for this case, you should learn to use PreparedStatement

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.