I have a node.js app using node-mysql to query a MySQL database.
我有一个节点。使用node-mysql查询MySQL数据库的js应用程序。
Problem: It appears that when I make the table name in the query a variable, things stop working. Did I miss out on something?
问题:当我将查询中的表名作为变量时,事情就停止了。我错过什么了吗?
Working Node Code
工作节点的代码
client.query('SELECT * from tableA',
function(error, results, fields) {
if (error)
throw error;
callback(results);
});
Non-working Node Code
非工作节点代码
client.query('SELECT * from ?',
[ tableA ],
function(error, results, fields) {
if (error)
throw error;
callback(results);
});
2 个解决方案
#1
3
You could probably just append the table name to the string (pseudo code, I don't know node.js)
可以将表名附加到字符串(伪代码,我不知道node.js)
client.query('SELECT * from ' + [tablaA],
function(error, results, fields) {
if (error)
throw error;
callback(results);
});
#2
0
They reason why it's not working is pretty clear.
他们的理由很清楚,为什么它不起作用。
The query from the non-working code will be :
来自非工作代码的查询将是:
SELECT * from 'tableA'
从“为多”选择*
A solution is the one from @Andreas, but you will have the same problem in a where statement or insert for other values you don't want to be escaped like "null" value. ( convert into a string)
解决方案是@Andreas的那个,但是对于where语句或插入其他不希望像“null”值那样转义的值,您会遇到同样的问题。(转换成字符串)
Same problem here
同样的问题在这里
Check out the source how format && escape from node-mysql works.
查看源文件如何从node-mysql工作。
#1
3
You could probably just append the table name to the string (pseudo code, I don't know node.js)
可以将表名附加到字符串(伪代码,我不知道node.js)
client.query('SELECT * from ' + [tablaA],
function(error, results, fields) {
if (error)
throw error;
callback(results);
});
#2
0
They reason why it's not working is pretty clear.
他们的理由很清楚,为什么它不起作用。
The query from the non-working code will be :
来自非工作代码的查询将是:
SELECT * from 'tableA'
从“为多”选择*
A solution is the one from @Andreas, but you will have the same problem in a where statement or insert for other values you don't want to be escaped like "null" value. ( convert into a string)
解决方案是@Andreas的那个,但是对于where语句或插入其他不希望像“null”值那样转义的值,您会遇到同样的问题。(转换成字符串)
Same problem here
同样的问题在这里
Check out the source how format && escape from node-mysql works.
查看源文件如何从node-mysql工作。