【问题标题】:You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册以获取正确的语法
【发布时间】:2021-05-12 19:14:08
【问题描述】:

每当我尝试测试我的端点的数据库时出现错误,它显示“代码”:“ER_PARSE_ERROR”

这是我的mysql数据库模型:

let query = `UPDATE tbl_branch_info 
            SET 
            ${whats === undefined ? `` : `whats=${whats},`}
            ${twitter === undefined ? `` : `twitter='${twitter}',`}
            ${facebook === undefined ? `` : `facebook='${facebook}',`}
            ${snapchat === undefined ? `` : `snapchat='${snapchat}',`}
            ${instagram === undefined ? `` : `instagram='${instagram}',`}
            ${phone === undefined ? `` : `telephone=${phone},`}
            ${address === undefined ? `` : `address='${address}',`}
            ${email === undefined ? `` : `email='${email}',`}
            ${city_id === undefined ? `` : `city_id=${city_id},`}
            ${category_id === undefined ? `` : `category_id=${category_id},`}
            ${subCategoryId === undefined ? `` : `sub_cat_id=${subCategoryId},`}
            ${lon === undefined ? `` : `lon=${lon},`}
            ${lat === undefined ? `` : `lat=${lat}`}     
            WHERE branch_id = ${branch_id}`;

它给了我这个错误:

error message from postman

但是如果我将经纬度的 branch_id 之前的最后一行更改为此

lat='${lat}'

它会很好地传递数据,那么这里可能是什么问题?

【问题讨论】:

  • 显示导致此错误消息的 query 值。
  • @Akina 我只是给出了一些值,并非全部用于测试此方法是否有效,我只给出了 branch_id、地址和 facebook,但就像我说的那样,如果我只更改 lat 它将正常工作,并且如果我删除它只制作 lon 它也会给出错误,所以我认为语法有问题或缺少某些东西,但我无法识别它
  • @Akina 我上传了一张图片到邮递员回复的帖子中
  • where 语句之前有一个额外的逗号,。使用 ORM 构建查询,您的方式并不安全。这也很复杂,如果你有一些可用的值而另一些为空。
  • @Shaharyar 哦,好吧,我想我开始明白你的意思了,因为早些时候我试图计算逗号,但我没有找到任何额外的逗号,非常感谢您的解释,不胜感激

标签: mysql sql node.js database express


【解决方案1】:
let query = `UPDATE tbl_branch_info 
            SET 
            ${whats === undefined ? `` : `whats=${whats},`}
            ${twitter === undefined ? `` : `twitter='${twitter}',`}
            ${facebook === undefined ? `` : `facebook='${facebook}',`}
            ${snapchat === undefined ? `` : `snapchat='${snapchat}',`}
            ${instagram === undefined ? `` : `instagram='${instagram}',`}
            ${phone === undefined ? `` : `telephone=${phone},`}
            ${address === undefined ? `` : `address='${address}',`}
            ${email === undefined ? `` : `email='${email}',`}
            ${city_id === undefined ? `` : `city_id=${city_id},`}
            ${category_id === undefined ? `` : `category_id=${category_id},`}
            ${subCategoryId === undefined ? `` : `sub_cat_id=${subCategoryId},`}
            ${lon === undefined ? `` : `lon=${lon},`}
            ${lat === undefined ? `` : `lat=${lat},`}    
            ${`branch_id = ${branch_id}`} 
            WHERE branch_id = ${branch_id}`;

【讨论】:

    猜你喜欢
    • 2018-11-27
    • 2019-10-24
    • 2017-02-11
    • 2016-06-17
    • 2020-02-22
    • 1970-01-01
    • 2020-02-05
    • 2017-11-22
    • 1970-01-01
    相关资源
    最近更新 更多