【问题标题】:Having troubles inserting multiple records at once without using for loop in sql server database using node.js在使用node.js的sql server数据库中没有使用for循环一次插入多条记录时遇到麻烦
【发布时间】:2022-01-05 18:10:59
【问题描述】:

大家好,我是在 node js 中使用 mysql 库的新手,我对在数据库中一次插入多行的可能性感兴趣。我制作了一个简单的代码,它从数据库表中捕获用户注册 ID,并使用该用户选择的角色索引。现在我想将用户和角色 id 添加到分别具有 user_id 和 role_id 的 Role_User 表中。

假设我想在不使用循环的情况下执行 3 次:

Insert Into User_role VALUES (1,20);

Insert Into User_role VALUES (2,20);

Insert Into User_role VALUES (3,20);

,其中第一列的数字 1、2、3 是角色索引。

Error I'm getting is RequestError: Incorrect syntax near '?'.

我怎样才能使这个查询工作?

if(req.body.roles){
                var sqlQuery = `SELECT ID from Test_user Where email = @email`;
                var indexiRola = findPositions(db.ROLES, req.body.roles);
                request.input('role', db.sql.NChar, req.body.roles);
                request.query(sqlQuery).then(
                    id => {
                        let ids = Array(req.body.roles.length).fill(id.recordset[0].ID);
                        console.log(ids); // [20, 20, 20]
                        let roleUsers = createSeperateLists(indexiRola, ids);
                        console.log(roleUsers); // [ [ 1, 20], [ 2, 20], [ 3, 20] ]
                        var insertQuery = `INSERT INTO Role_User ("role_ID", "user_id") VALUES ?`;
                        request.query(insertQuery, [roleUsers], function(err, result){
                            if(err) throw err;
                            console.log("Successfull insertion");
                        });
                    })
                }

【问题讨论】:

  • 插入 testuser(用户名,密码) VALUES ('user1', 1234), ('user2', 1234), ('user3', 1234);这是多行同时插入的sql查询,表示值需要插入数组类型数据

标签: javascript mysql sql node.js


【解决方案1】:

请检查它是否有帮助。当然,我无法检查它是否运行。

相反,它并不完整! request.query 是异步的,必须修改代码。

至少,如我所愿,这段代码不会闯入RequestError: Incorrect syntax near '?'.

请:

  • 检查修改是否适用于一个输入
  • 当您获得完整的解决方案时,请与我们分享
    if (req.body.roles) {
        var sqlQuery = `SELECT ID from Test_user Where email = @email`;
        var indexiRola = findPositions(db.ROLES, req.body.roles);
        request.input('role', db.sql.NChar, req.body.roles);
        request.query(sqlQuery).then(
            id => {
                let ids = Array(req.body.roles.length).fill(id.recordset[0].ID);
                console.log(ids); // [20, 20, 20]
                let roleUsers = createSeperateLists(indexiRola, ids);
                console.log(roleUsers); // [ [ 1, 20], [ 2, 20], [ 3, 20] ]
                // I removed the double quotes from the names and,
                // VALUES (?, ?) - this is the right syntax for SQL
                const insertQuery = 'INSERT INTO Role_User (role_ID, user_id) VALUES (?, ?)';
                // Then, forEach roleUsers
                // By the way, [roleUsers] yields an array of arrays of arrays, not cool
                roleUsers.forEach(
                    roleUser => {
                        request.query(
                            insertQuery,
                            roleUser,
                            // Oh! Shoot!!! This is async.
                            // Man, you will have to deal with it.
                            // Please, share with us the final solution!
                            function(err, result) {
                                if (err) throw err;
                                console.log("Successfull insertion");
                            }
                        );
                    }
                );
            })
    }

【讨论】:

  • 仍然收到以下错误:UnhandledPromiseRejectionWarning: RequestError: Incorrect syntax near '?'。
猜你喜欢
  • 2016-08-17
  • 2023-04-03
  • 2016-11-27
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 2016-09-19
相关资源
最近更新 更多