Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

node.js - mysql2 nodejs pass table name as variable / right syntax to use near '??'

Hi I am trying to run a query but getting the following error: Nodej Mysql2

query

tableName = "some_table"
id = "some_id";

const readForwardedMessageDetails = pool.execute('SELECT * FROM ?? WHERE `id` = ? LIMIT 1',
        [tableName, id],
        function(err, row) {
            if (err) {
                console.log(err)
                return
            } else {
            
            }
            
            code: 'ER_PARSE_ERROR',
                errno: 1064,
                sqlState: '42000',
                sqlMessage: "You have an error in your SQL syntax; check the manual 
            that corresponds to your MariaDB server version
            for the right syntax to use
            near '?? WHERE `id` = ? LIMIT 1'
            at line 1 ",
            sql: 'SELECT * FROM ?? WHERE `id` = ? LIMIT 1'

Was trying from here : Nodejs-Mysql Query table name as a variable

Also use of ?? double question marks for tablename is given here : https://github.com/mysqljs/mysql#preparing-queries

MySql2 suggests it is mostly compatible with mysql so tried the answers above. https://github.com/sidorares/node-mysql2#history-and-why-mysql2 : MySQL2 is mostly API compatible with mysqljs and supports majority of features. MySQL2 also offers these additional features (hence the inference)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had raised the issue with MySql2 since they have mentioned on their website to report any incompatibilities with MySql https://www.npmjs.com/package/mysql2#api-and-configuration stating If you find any incompatibility with Node MySQL, Please report via Issue tracker. We will fix reported incompatibility on priority basis.

Their response is https://github.com/sidorares/node-mysql2/issues/1435

while similar to the .query() .execute() syntax is different and only supports what valid input for PREPARE is ( see https://dev.mysql.com/doc/refman/8.0/en/prepare.html ). You can get your ?? interpolated on the client side in a separate step or just switch to non-prepares statements call with .query()

Passing table name in query is just fine depending on your program requirement especially reusable components can be made where table names are switched/column names are switched. For eg. if you have comments table for different entities and maintaining different tables for these (imagine how one big comment table would perform for the whole website)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...