Update postgreql database with Node.js

2019-08-23 05:48发布

问题:

I try to Update a table on postgresql so for that i'm creating two arrays

    var name_ = []; 
    var id_ = [];

then i creat a forEach loop where MyRow is the query that contain different data

    MyRow.rows.forEach(function(row){ 
      name_.push(row.name);
      id_.push(row.id);                      
    });            
    var updateAccount = 'UPDATE myTable SET myRow =$1 '+
    'where id = $2'   
    client.query(updateAccount,[name_.toString(),id_.toString()],false).then(function(err,result){                      
        if (err) {
            console.log(err.stack);        
          } else {
            console.log(name_)
          }
    });

then i had an error error: missing FROM-clause entry for table "myTable "

回答1:

I'm not sure the error is related, but where is shema mentioned? Also use backticks, oneLine and const on template strings.

const updateAccount = oneLine`
   UPDATE myTable
   SET myRow = $1
   WHERE shema.id = $2  -- where is `shema` defined?
`;


回答2:

My guess is that you should enclose your values in single quotes.

var updateAccount = "UPDATE myTable SET myRow = '$1' "+
"where shema.id = '$2'"

In your example, your values are interpreted as column names, so PostgreSQL asks you to specify a table containing these.