简体   繁体   English

无法在节点js中使用util.format()在mysql准备的语句中解析整数

[英]Can't parse integer in mysql prepared statement using util.format() in node js

I am using util module in node.js for formatting strings. 我在node.js中使用util模块格式化字符串。 But I found a catch when I wanna run an SQL Query like - 但是当我想运行一个SQL查询时发现了一个问题-

select * from something LIMIT 5 OFFSET 0;

So what I am trying to do is, I've created a queryString then format that queryString and finally execute the query through server application. 因此,我想做的是,创建了一个queryString,然后格式化该queryString,最后通过服务器应用程序执行查询。 Here is the code in nodejs : 这是nodejs中的代码:

queryString = "select * from something LIMIT '%d' OFFSET '%d'";
query = util.format(queryString, 5, 0);
res = db.executeQuery(query);

I have also tried %i and %s, with quotes and without quotes but nothing worked for me. 我也尝试了%i和%s,使用引号和不使用引号,但没有任何用处。 Here is the log : 这是日志:

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%i OFFSET %i' at line 1

I am sure I am missing something here. 我确定我在这里错过了一些东西。 Thanks in advance 提前致谢

Assuming you are using mysql module 假设您正在使用mysql模块

There are other ways: 还有其他方法:

var limit = 5;
var offset = 0;
var qs = `select * from something LIMIT ${limit} OFFSET ${offset}`;

or 要么

connection.query('select * from something LIMIT ? OFFSET ?', [limit, offset], function (error, results, fields) {
  ...
});

or 要么

var sql = mysql.format('select * from something LIMIT ? OFFSET ?', [limit, offset]);

See docs 查看文件

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

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