插入时sql查询失败

Posted

技术标签:

【中文标题】插入时sql查询失败【英文标题】:sql query failing on insert 【发布时间】:2021-07-10 02:38:08 【问题描述】:

我有这个简单、标准和简单的 sql 查询,但我无法找出问题所在。看起来还不错,但无法找出问题所在...它给了我一个错误(您可以在下面找到),但首先,这是插入查询。

//DB2 CONNECTION FOR ADDING PRODUCTS
var addProducts =
  "insert into PRODUCTS ( ITEM, DESCRIPTION, PRICE, SIZES, DIVISION, XS, S, M, L, XL, INTER, EACH_CASE ) VALUES ('" +
  req.body.item +
  "', '" +
  req.body.description +
  "', '" +
  req.body.price +
  "', '" +
  allSizes +
  "', '" +
  req.body.category +
  "', '" +
  req.body.xs +
  "', '" +
  req.body.s +
  "', '" +
  req.body.m +
  "', '" +
  req.body.l +
  "', '" +
  req.body.xl +
  "', '" +
  inter +
  "', '" +
  each_case +
  "' )";
 
    ibmdb.open(req.session.ibmdbconnDash, function (err, conn) 
      if (err) return console.log(err + "getting error here 1");
      conn.query(addProducts, function (err, rows) 
        if (err) 
          console.log(err + "getting error here 1");
        

        var getProductDetails = "select * from products where ITEM = '" + req.body.item + "'";
        ibmdb.open(req.session.ibmdbconnDash, function (err, conn) 
          if (err) return console.log(err + "getting error here 2");
          conn.query(getProductDetails, function (err, gianluca) 
            if (err) 
              console.log(err + "getting error here 2");
            

            var productAddedValue = ""
            console.log(gianluca)
            res.render("add-products2", 
              page_title: "add-products2",
              data: gianluca,
              userName: req.session.username,
              FN: req.session.firstname,
              LN: req.session.lastname,
              CO: req.session.company,
              productAddedValue: productAddedValue,
            );

            conn.close(function () 
              console.log("closed the function /add-products p2");
            );
          );

); 它给了我这个错误:

[Error: [IBM][CLI Driver][DB2/LINUXX8664] SQL0010N  The string constant beginning with "' )" does not have an ending string delimiter.  SQLSTATE=42603
] 
  error: '[node-ibm_db] SQL_ERROR',
  sqlcode: -10,
  state: '42603'

不知道如何解决这个问题!感谢您的帮助!

【问题讨论】:

你能附上你用来向数据库发送命令的查询吗? @JaisonThomas 刚刚更新了问题 使用准备好的语句 (conn.prepare) 而不是查询 (conn.query)。使用准备好的语句修复了由于 req.body 值中的单引号引起的错误。 【参考方案1】:

假设您使用的是 nodejs es5,这应该可以工作,而且更干净

const addProducts =  `insert into PRODUCTS (ITEM, DESCRIPTION, PRICE, SIZES, DIVISION, XS, S, M, L, XL, INTER, EACH_CASE) VALUES ('$req.body.item', '$req.body.description', '$req.body.price', '$allSizes', '$req.body.category', '$req.body.xs', '$req.body.s', '$req.body.m', '$req.body.l', '$req.body.xl', '$inter', '$each_case');`;

【讨论】:

感谢您的尝试,但仍然给了我同样的错误! IBM 是否有一个 IDE,您可以在其中测试原始字符串中的相同值? 我通过在 SQL 字符串末尾添加 ; 更新了我的回复 仍然发生。一样。我很困惑为什么,你的回答对我来说很有意义,哈哈。是的,他们确实这样做了,它似乎在那里工作,代码有问题,想知道为什么.... 也许控制台记录您的查询,看看它是否符合您的预期?【参考方案2】:

原来,这是一个愚蠢的错误。当我将数据插入数据库时​​,我在描述列中添加了一个',这让整个事情都被抛弃了。谢谢大家4的帮助

【讨论】:

以上是关于插入时sql查询失败的主要内容,如果未能解决你的问题,请参考以下文章

使用带有 Azure 链接表的 Access 插入 SQL 失败

使用 SQLKorma 获取语法异常:使用 SQL 执行查询失败

SQL查询错误:转换varchar值时转换失败

从 R 执行时 SQL Server 查询失败

T-SQL 谜题:将其用作内联视图时查询失败

使用 Monitor 线程插入查询时 SQL 中的死锁 [关闭]