我在java中编写代码,例如
Double Alish = Double.valueOf(productAlishText.getText().toString());
Double Satish = Double.valueOf(productSatishText.getText().toString());
Query = "INSERT INTO MALLAR (CUSTOMERID,PRODUCTNAME,PRODUCTALISH,PRODUCTSATISH) "
+ "VALUES ('" + getEkspeditorIndex() + "','" + productAdiText.getText() + "','" + Alish+ "','" + Satish+ "')";
stat.execute(Query);
但是此代码会出现此错误。
SQLSyntaxErrorException:类型为' DOUBLE'的列不能保持价值观 类型' CHAR'
答案 0 :(得分:1)
您也引用了Double
个值。删除引号。
Query = "INSERT INTO MALLAR (CUSTOMERID,PRODUCTNAME,PRODUCTALISH,PRODUCTSATISH) "
+ "VALUES ('" + getEkspeditorIndex() + "','" + productAdiText.getText()
+ "'," + Alish+ "," + Satish+ ")";
请注意,上面的''
和Alish
周围没有引号Satish
。
但是,我建议你改用PreparedStatement
。您不仅可以摆脱这种连接问题,还可以防止SQL注入攻击。
PreparedStatement pstmt = connection.prepareStatement("INSERT INTO MALLAR "
+ "(CUSTOMERID, PRODUCTNAME, PRODUCTALISH, PRODUCTSATISH) VALUES (?, ?, ?, ?)");
pstmt.setString(1, getEkspeditorIndex());
pstmt.setString(2, productAdiText.getText());
pstmt.setDouble(3, Alish);
pstmt.setDouble(4, Satish);
pstmt.executeUpdate();