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

Fahad Subzwari
  • 2,109
  • 3
  • 24
  • 52
  • That's explained in the documentation, start [here](https://github.com/mysqljs/mysql#escaping-query-identifiers) and work your way down. – robertklep Jul 31 '17 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 – Fahad Subzwari Jul 31 '17 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. – robertklep Jul 31 '17 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? – Fahad Subzwari Jul 31 '17 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. – robertklep Jul 31 '17 at 18:15
  • Now i cleared that i want to know both. how to pass object and table name as variable in mysql-node query. can you tell me? – Fahad Subzwari Jul 31 '17 at 18:21

2 Answers2

10

Try this:

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

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

robertklep
  • 198,204
  • 35
  • 394
  • 381
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";
imatwork
  • 523
  • 6
  • 16