简体   繁体   English

使用 node.js 在 sql 服务器数据库中一次插入多条记录时遇到麻烦

[英]Having troubles inserting multiple records at once without using for loop in sql server database using node.js

Hello guys I'm new in using mysql library in node js and I'm interested in possibility of inserting multiple rows at once in database.大家好,我是在节点 js 中使用 mysql 库的新手,我对在数据库中一次插入多行的可能性感兴趣。 I've made a simple code which captures user registration id from database table and uses indexes of roles which that user has selected.我制作了一个简单的代码,它从数据库表中捕获用户注册 ID,并使用该用户选择的角色索引。 And now I want to add user and role id into Role_User table which has user_id and role_id respectively.现在我想将用户和角色 ID 添加到分别具有 user_id 和 role_id 的 Role_User 表中。

Let's say I wanna do this 3 times without using loop:假设我想在不使用循环的情况下执行 3 次:

Insert Into User_role VALUES (1,20);

Insert Into User_role VALUES (2,20);

Insert Into User_role VALUES (3,20);

, where numbers 1,2,3 of the first columns are role indexes. ,其中第一列的数字 1、2、3 是角色索引。

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

How can I make this query work?我怎样才能使这个查询工作?

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");
                        });
                    })
                }

Check if it helps, please.请检查它是否有帮助。 I cannot check it running, of course.当然,我无法检查它是否运行。

It's not complete, on the contrary!相反,它不完整! request.query is async, and that code has to be revised. request.query是异步的,必须修改该代码。

At least, this code, as I hope, does not break into RequestError: Incorrect syntax near '?'.至少,正如我希望的那样,这段代码不会闯入RequestError: Incorrect syntax near '?'.

Please:请:

  • check if the revision works for one input检查修订是否适用于一个输入
  • whenever you get the complete solution, share it with us每当您获得完整的解决方案时,请与我们分享
    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");
                            }
                        );
                    }
                );
            })
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用mongoose和node.js在mongoDB中插入文档时遇到问题 - Having trouble inserting documents in mongoDB using mongoose and node.js Node.js puppeteer mysql-使用mysql将获取的值插入到循环内的数据库中 - Node.js puppeteer mysql - Inserting fetched values in database inside a loop using mysql 将多条记录插入 node.js 中的 SQL 服务器(mssql) - Insert multiple records into SQL Server (mssql) in node.js 使用Node.js将多个值插入或更新到MySQL表中 - Inserting or Updating multiple values into a MySQL table using Node.js 使用node.js将多个文档插入Mongodb - Inserting multiple documents to Mongodb using node.js 如何使用node.js和连接池一次连接到SQLServer数据库? - How to make multiple connections at once to a SQLServer database by using node.js and connection pooling? 数据未使用node.js插入mongodb - data is not inserting into mongodb using node.js 使用Q链接使用Node.js服务器的数据库查询 - Using Q to chain Database queries using Node.js server 我无法使用 Node.JS 服务器连接到 SQL 服务器 - I cannot connect to SQL server using Node.JS server 尝试使用 Node.js MSSQL 连接到 SQL 服务器数据库时出现错误 ELIFECYCLE 3228369023 - Error ELIFECYCLE 3228369023 on attempted connection to SQL Server database using Node.js MSSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM