4

How can i pass table name as variable. Basically i want to make e function in which i will take table name as a parameter and object insert record in mysql database in using nodejs My function will be like

exports.insertIntoDb = function(tableName,insertObj) {
 connection.query('INSERT INTO administrator SET ?',insertObj, function(error, result, fields) {
    if(error){
        res.json({
            status:false,
            message:'There is some problem with query'
        })
    }
    else{
        res.json({
            status : true,
            data : result,
            message: 'user registered successfully'
          })    
       }
  });
 }

But i am wondering that how to pass table name in this query which is parameter taken from function. I am asking about syntax? I am using nodejs-mysql

6
  • That's explained in the documentation, start here and work your way down. Commented Jul 31, 2017 at 17:41
  • @robertklep i found this query close to my solution var userId = 1; var columns = ['username', 'email']; var query = connection.query('SELECT ?? FROM ?? WHERE id = ?', [columns, 'users', userId], function (error, results, fields) { if (error) throw error; // ... }); But in my case i am making insert query and i want to send object in sql query. and in this query there is no method of how to place object in query. In this query he is inserting columns as an array but i want to send colunms as a object and table name Commented Jul 31, 2017 at 18:08
  • In that case, you need to reformulate your question, because you're asking (repeatedly) how to pass the table name into the query and nothing about passing an object. Commented Jul 31, 2017 at 18:11
  • @robertklep please read my question carefully i mentioned that i want to pass object in sql insert query and table name as a variable. so can you guide me and alter my query and show me ho to do this? Commented Jul 31, 2017 at 18:13
  • "But i am wondering that how to pass table name in this query which is parameter taken from function". That's what you're asking. That's also in the title of your question. Commented Jul 31, 2017 at 18:15

2 Answers 2

10

Try this:

exports.insertIntoDb = function(tableName,insertObj) {
  connection.query('INSERT INTO ?? SET ?', [ tableName, insertObj ], ...)
};

Documented here: https://github.com/mysqljs/mysql#preparing-queries

Sign up to request clarification or add additional context in comments.

2 Comments

thanx :) your solution helped me out.
getting an error please see here : stackoverflow.com/q/69716876/3904109
0

Inside app.js:

app.put('/updateCell', async function(req, res) {
    console.log("REST: PUT /updateCell");
    let orderInfo = req.body;
    let cellValue = orderInfo.cell;
    let CustomerName = orderInfo.CustomerName;
    let ColumnName = orderInfo.columnName;

    connection.query("UPDATE vehicles SET ?? = ? WHERE order_CustomerName = ?", [columnName, cellValue, customerName],
        function(err, result) {
          if (err) throw err;
        });
        res.send();
});

example:

//cellValue = "Fluffiest hat of them all";
//customerName = "Jenny Hopkins";
//columnName = "description";

So the SQL query would be the same as:

UPDATE order SET description = "fluffiest hat of them all" WHERE order_CustomerName = "Jenny Hopkins";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.