Sunday Daniel Sunday Daniel - 1 month ago 6
Java Question

SqLite Error in Javafx

I have a problem with updating my database after calling a method to sum a new credit balance.

[SQLITE_ERROR] SQL error or missing database (near "'18.0'": syntax error)

This is my query :

try {
String query = "UPDATE cards SET cardBalance '"+newCreditBalance+"' WHERE cardID = idList.getValue().toString";
PreparedStatement pst = connection.prepareStatement(query);


Did I make any mistake with '"+newCreditBalance+"'? It should be a double value in both java and my database.

Answer

The syntax error is because you're missing an equals sign: "UPDATE cards SET cardBalance="+newCreditBalance.

However, it's a very bad idea to use string concatenation like that to put values into an SQL query or statement (because of the danger of SQL injection). Instead, pass a string like this to prepareStatement(): UPDATE cards SET cardBalance = ? WHERE cardID = ?"

And then call the relevant setter methods before actually executing the query:

pst.setFloat(1, newCreditBalance);
pst.setString(2, idList.getValue().toString);

You should also be using the correct type for idList.getValue(), but from your example I don't know what that should be.

Comments