简体   繁体   中英

What if too many parameters in mysql query in node.js

This table has many parameters, and when i do insert it's like this, (... is for demo propose)

    const sqlCmd = `insert into Consumer (key, secret, ..., version)
                values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;

try {
    const param = [myTable.key, myTable.secret, ..., myTable.version];
    return await dbPool.execSqlCmd(sqlCmd, param)
}

Is there a way to avoid so many ?s ?

In SQL, you use one ? parameter placeholder for each scalar value you want to pass to the SQL statement. No more, no less.

If you want fewer ? placeholders, then you must pass fewer parameters. You can do this by:

  • Omitting some columns from the INSERT statement. The row inserted will use the DEFAULT value declared for the column, or else NULL if there is no default.
  • Using a constant expression in the VALUES clause for some columns, instead of a parameter. I mean this can be a constant literal, like a numeric or string or date, or it can be an expression or function, which will be evaluated, and the resulting value used for the INSERT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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