where子句中的未知列“ sunny”

时间:2019-03-23 10:49:22

标签: java jdbc

import React from 'react';
import "./style.scss";

function InFrige(props) {

  const handleRemove = (e, counts) => {
    console.log(e.target.name)
  }

  const displayFood = () => {
     var counts = {};
    props.foodAddedByUser.forEach(function(x) { counts[x] = (counts[x] || 0) + 1; });

    let foodItems = []
    for(var index in Object.entries(counts)){
        foodItems.push(
           <div className="inFrige-food-item" key={index}>
            <h3>{Object.keys(counts)[index]} x{Object.values(counts)[index]}</h3>
             <button onClick={handleRemove} name={Object.keys(counts)[index]}>Remove</button>
           </div>
         )
    }
  return foodItems
  }

  return (
    <div>
      <div className="inFrige-food-container">
          {displayFood()}
      </div>
    </div>
  )
 }

 export default InFrige;

错误:     java.sql.SQLSyntaxErrorException:您的SQL错误     句法;检查与您的MariaDB服务器相对应的手册     在第1行的“按名称”附近使用正确语法的版本

1 个答案:

答案 0 :(得分:4)

  

我猜想这行中的问题字符串q =“选择汽车的pcode,pname,pprice,其中us =” + name;

是的,因为名称(显然是阳光)不在SQL引号中,所以它看起来像是列引用。

从不使用字符串连接为SQL查询添加值。请改用prepared statements

PreparedStatement ps = con.prepareStatement("select pcode ,pname,pprice from car where us = ?");
ps.setString(1, name);
ResultSet rs = ps.executeQuery();

这样,信息就作为数据处理,而不是作为SQL处理(必要时适当地转义,等等)。那里要注意的事情:

  1. 您可以使用?值所在的位置。即使您要使用字符串作为值,也请不要?用引号引起来。
  2. 您可以通过在连接上调用prepareStatement来获得准备好的语句。
  3. 不要将字符串传递到executeQuery中。 (这很重要,因为遗憾的是,您可以将字符串传递到executeQuery上的PreparedStatement中,从而绕过了使用准备好的语句的整个过程;应该将其定义为导致一个例外,但可惜不是这样。)

让我向您介绍my friend Bobby

Her daughter is named Help I'm trapped in a driver's license factory.