Using nodejs to update the table of mysql, the parameters after SET are not fixed, how to achieve? Add SET after?? How do you understand it?

< H2 > question 1 < / H2 >

I want to write an interface to update user information,

UPDATE user_information SET the parameters here are not fixed where uid =?

could be name=? or name=?, sex=? or name=?, sex=?, qq=?

Please how should I achieve it?

< H2 > question 2 < / H2 >

when using nodejs to operate a database, SET is followed by?? What does it mean?

UPDATE demo SET? Where id=?

app.all("/information/update", (req, res, next) => {
    let sess = req.session;
    let result = {
        code: "0000",
        data: {},
        message: "fail",
        detail: "",
        function: "prize",
        number: 0
    };
    let params = req.body;

    let selectSql = "select name from user_information where uid = ?";
    let updateSql = "UPDATE user_information SET name=? where uid = ?";



    let name = helper.stringTrim(params.name) || "";

    if(sess.loginUserInfo === undefined) {
        result["detail"] = "";
        res.send(JSON.stringify(result));
    } else {
        let loginInformation = sess.loginUserInfo;
        // console.log("loginInformation == ", loginInformation);
        pool.getConnection((err, connection) => {
            connection.query(
                selectSql,
                [loginInformation.uid],
                (err, rows) => {
                    // console.log("rows ==", rows);
                    if(rows.length === 1) {
                        result["detail"] = "";
                        connection.query(
                            updateSql,
                            [name, loginInformation.uid],
                            (err, rows) => {
                                if(err) {
                                    result["detail"] = "";
                                    res.send(JSON.stringify(result));
                                } else {
                                    result["detail"] = "0001";
                                    result["message"] = "success";
                                    result["detail"] = "";
                                    res.send(JSON.stringify(result));
                                }

                            }
                        );

                    } else {
                        result["detail"] = "";
                        res.send(JSON.stringify(result));
                    }

                }
            );
            pool.releaseConnection(connection);
        })
    }
});
Jun.18,2022

< H2 > solution < / H2 >
  • question 1
follow ? directly after set, and then fill in the dictionary you want to enter in the following parameters
// This escaping allows you to do neat things like this:
var post  = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function (error, results, fields) {
  if (error) throw error;
  // Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
  • question 2
? with fields that represent the escape you need, such as column name or table name
  https://www.npmjs.com/package...

Menu