문제

I have a node.js app using node-mysql to query a MySQL database.

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);
                });
도움이 되었습니까?

해결책

You could probably just append the table name to the string (pseudo code, I don't know node.js)

client.query('SELECT * from ' + [tablaA],
                function(error, results, fields) {
                    if (error)
                        throw error;
                    callback(results);
                });

다른 팁

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)

Same problem here

Check out the source how format && escape from node-mysql works.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top