如何在Node.js中使用参数化查询

huangapple go评论70阅读模式
英文:

How to use paramerized query in Nodejs

问题

I am working with Node.js and Express.js. Right now, I am trying to execute a select query, and the query is working successfully. However, I want two things:

  1. How to convert this query to a "parameterized query"?
  2. Whenever I am trying to get "total rows," I am getting "[object object]."

Here is my current code:

static async addServiceDetail(shopId, serviceId, genderTypeId, price, serviceName) {
    const sql = `SELECT id FROM hc_servicesdetail WHERE shopId = ? AND serviceId = ? AND genderTypeId = ?`;
    const [rows, fields] = await pool.execute(sql, [shopId, serviceId, genderTypeId]);
    console.log('total rows are ' + rows); // getting [object object]
    if (rows < 1) {
        // further code
    }
}

请注意,我已经将查询转换为了参数化查询,使用了占位符?,并将参数传递给pool.execute方法。希望这有助于解决您的问题。

英文:

I am working with Nodejs and expressjs,Right now i am trying to execute select query , query is working successfully but i want two things

1) How to convert this query to &quot;paramerized query&quot; ?
2) Whenever i am trying to get &quot;total rows&quot; then i am getting &quot;[object object] &quot;

Here is my current code

static async addServiceDetail(shopId,serviceId,genderTypeId,price,serviceName)
    {
        const sql = `SELECT id from hc_servicesdetail WHERE shopId=&#39;${shopId}&#39; AND  serviceId=&#39;${serviceId}&#39; AND genderTypeId=&#39;${genderTypeId}&#39;`;
        const [rows, fields] = await pool.execute(sql);
         console.log(&#39;total rows are&#39;+ rows); // getting [object object]
        if(rows&lt;1)
            {
                  //further code  
            }
    }

答案1

得分: 1

你可以将参数作为数组传递给execute调用,然后将行记录到控制台:

let shopId = 'B';
let serviceId = 'k';
let genderTypeId = 'F';
let sql = 'SELECT id from hc_servicesdetail WHERE shopId=? AND serviceId=? AND genderTypeId=?';
const [rows, fields] = await pool.execute(sql, [shopId, serviceId, genderTypeId]);
console.log('Rows:', rows)

输出将类似于以下内容:

Rows: [ { id: 1 } ]
英文:

You can pass parameters as an array to the execute call, then log the rows to the console:

let shopId = &#39;B&#39;;
let serviceId = &#39;k&#39;;
let genderTypeId = &#39;F&#39;;
let sql = &#39;SELECT id from hc_servicesdetail WHERE shopId=? AND serviceId=? AND genderTypeId=?&#39;;
const [rows, fields] = await pool.execute(sql, [shopId, serviceId, genderTypeId]);
console.log(&#39;Rows:&#39;, rows)

The output will look something like this:

Rows: [ { id: 1 } ]

答案2

得分: 0

For printing the value that is contained inside results just use results.

console.log(rows)

英文:

For printing the value that is contained inside results just use results.

console.log(rows)

huangapple
  • 本文由 发表于 2023年5月11日 01:17:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定