简体   繁体   English

Node.js SQL 准备查询的服务器单引号问题

[英]Node.js SQL Server single quote issue with prepared query

I'm trying to pull data from a SQL Server database using node.js, and passing the data to Google Chatbot.我正在尝试使用 node.js 从 SQL 服务器数据库中提取数据,并将数据传递给 Google Chatbot。

This is what my SQL Server query looks like这就是我的 SQL 服务器查询的样子

INSERT INTO @Return
    SELECT 'You' + '\' + 've assigned ' + CONVERT(varchar(4), COUNT(1)) + ' to yourself'

This is the function that sends the data to the Google Chatbot.这是将数据发送到 Google Chatbot 的 function。

function GbWebhookFunction(){

    this.sendMessage = async function(strMessage){
        console.log(strMessage);
        fetch(webhookURL, {
            method: 'POST',
            headers: {
            'Content-Type': 'application/json; charset=UTF-8',
            },
            body: "{'text': '" + strMessage + "'}",
        }).then((response) => {
            console.log(response);
        });
    };
};

This outputs:这输出:

You\ve assigned X to yourself

I tried to add to my function the code below but that did not work.我试图将下面的代码添加到我的 function 中,但这没有用。

strMessage = strMessage.replace("\", "'");

I removed one \ from my SQL query but that outputted:我从我的 SQL 查询中删除了一个\但输出:

Youe assigned X to yourself

Output when I remove \ from the SQL query Output 当我从 SQL 查询中删除\

Any ideas on how I can replace the \ with a single quote?关于如何用单引号替换\的任何想法?

In your query you forgot to add the single quote.在您的查询中,您忘记添加单引号。 The query should most likely be查询很可能是

INSERT INTO @Return
    SELECT 'You\'ve assigned ' + CONVERT(varchar(4), COUNT(1)) + ' to yourself'

Edit编辑

Based on some provided errors I would instead suggest using the double single quotes.基于一些提供的错误,我建议使用双单引号。 Which means the SQL query should be as followed:这意味着 SQL 查询应如下所示:

INSERT INTO @Return
    SELECT 'You''ve assigned ' + CONVERT(varchar(4), COUNT(1)) + ' to yourself'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM