0

I tried to add a record do MariaDB database use Sql2o with IntelliJ

All fields have type String and in Database have type varchar.

But I got the error below (java.sql.SQLSyntaxErrorException)

Error in executeUpdate, You have an error in your SQL syntax; check the manual that corresponds to 
your MariaDB server version for the right syntax to use near 'desc, manufacturer, category, 
condition) values ('4082304923748','iPhone X','...' at line 1
public static void add(Product product, HttpServletRequest request) {
        final String sql = "insert into product (item_code, name, unit_price, units_in_stock, desc, manufacturer, category, condition) " +
                "values (:item_code,:name,:unit_price,:units_in_stock,:desc,:manufacturer,:category,:condition)\n";
        try (Connection con = sql2o.open()) {
            //con.createQuery(sql).bind(product).executeUpdate();
            con.createQuery(sql).addParameter("item_code", product.getItem_code())
                    .addParameter("name", product.getName())
                    .addParameter("unit_price", product.getUnit_price())
                    .addParameter("units_in_stock", product.getUnits_in_stock())
                    .addParameter("desc", product.getDesc())
                    .addParameter("manufacturer", product.getManufacturer())
                    .addParameter("category", product.getCategory())
                    .addParameter("condition", product.getCondition())
                    .executeUpdate();
        }
}
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
LeeHari
  • 31
  • 7
  • Does this answer your question? [Strange syntax error when changing type of a column](https://stackoverflow.com/questions/28879848/strange-syntax-error-when-changing-type-of-a-column) – Bill Karwin Aug 18 '21 at 16:58
  • No, I want to insert. Not change anything – LeeHari Aug 18 '21 at 17:03
  • 1
    Yes, but `DESC` is a reserved keyword. You can't use it as a column name unless you delimit it in back-ticks. That's the point of that answer. Also see https://mariadb.com/kb/en/reserved-words/ – Bill Karwin Aug 18 '21 at 17:05
  • 1
    It would be simpler to just choose a different word for your column name, a word that is not one of the reserved keywords (see that link for a list). – Bill Karwin Aug 18 '21 at 17:05
  • Oh, this's a problem. Besides `CONDITION` is also a reverved-keyword. – LeeHari Aug 18 '21 at 17:15

1 Answers1

0

DESC and CONDITION is a reserved keyword, can't use them as a column name

for more

LeeHari
  • 31
  • 7