简体   繁体   中英

Update SQL statement with multiple fields within ONE table

I am trying to write an update SQL statement for many columns within one table only. For example product table. Within product table, there are many columns like name, description, price, quantity, image, category, status.

So I came out with this SQL statement:

String sql = "UPDATE sm_product SET productDescription = '" + desc +
    "' , productPrice = ' + price + ', productQuantity = ' + quantity +
    ', productImage = '" + image + "', productCategory = '" + category +
    '"  WHERE productName = '" + name + "'";

However, the compiler told me that there are unclosed character literal and not a statement. I wonder how should I fix this SQL statement because I only have one table to update. But within that table, there are many fields.

Thanks in advance.

It looks like you have problems with your quotes. Try this:

String sql = "UPDATE sm_product SET productDescription = '" + desc +
    "' , productPrice = " + price + ", productQuantity = " + quantity +
    ", productImage = '" + image + "', productCategory = '" + category +
    "'  WHERE productName = '" + name + "'";

This is assuming that price and quantity are numeric and the rest are strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM