英文:
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:
- How to convert this query to a "parameterized query"?
- 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 "paramerized query" ?
2) Whenever i am trying to get "total rows" then 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='${shopId}' AND serviceId='${serviceId}' AND genderTypeId='${genderTypeId}'`;
const [rows, fields] = await pool.execute(sql);
console.log('total rows are'+ rows); // getting [object object]
if(rows<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 = '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)
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论