如何使用node.js在mySQL中进行批量插入

如何使用node.js在mySQL中进行批量插入,mysql,node.js,Mysql,Node.js,如果使用以下内容,如何将大容量插入mySQL 使用嵌套数组可以进行批量插入,请参阅 嵌套数组被转换为分组列表(用于批量插入),例如。 ['a','b',['c','d']变成('a','b'),('c','d') 您只需插入一个嵌套的元素数组 文中给出了一个例子 注意:values是包装在数组中的数组数组 还有一个完全不同的批量插入包Ragnar123的所有道具 在Josh Harington提出关于插入ID的问题后,我想对其进行扩展 这些将是连续的。看看这个答案: 因此,您可以这样做(注意我对

如果使用以下内容,如何将大容量插入mySQL
使用嵌套数组可以进行批量插入,请参阅

嵌套数组被转换为分组列表(用于批量插入),例如。
['a','b',['c','d']
变成
('a','b'),('c','d')

您只需插入一个嵌套的元素数组

文中给出了一个例子

注意:
values
是包装在数组中的数组数组


还有一个完全不同的批量插入包

Ragnar123的所有道具

在Josh Harington提出关于插入ID的问题后,我想对其进行扩展

这些将是连续的。看看这个答案:

因此,您可以这样做(注意我对result.insertId所做的操作):

var语句='插入到??(“+sKeys.join()+”)值;
var insertStatement=[tableName,values];
var sql=db.connection.format(语句,insertStatement);
db.connection.query(sql,函数(错误,结果){
如果(错误){
返回clb(错误);
}
var rowIds=[];
for(var i=result.insertId;i
(我从一个称为PersistentObject的对象数组中提取了这些值。)


希望这有帮助。

如果拉格纳的答案对你不合适。以下可能是原因(根据我的经验)-

  • 我没有像我的
    Ragnar
    那样使用
    node-mysql
    包。我使用的是
    mysql
    包。他们是不同的(如果你没有注意到的话——就像我一样)。但我不确定这是否与
    不起作用有关?
    不起作用,因为它似乎对许多使用
    mysql
    包的人起作用

  • 尝试使用变量而不是

  • 以下几点对我有用-

    var mysql = require('node-mysql');
    var conn = mysql.createConnection({
        ...
    });
    
    var sql = "INSERT INTO Test (name, email, n) VALUES :params";
    var values = [
        ['demian', 'demian@gmail.com', 1],
        ['john', 'john@gmail.com', 2],
        ['mark', 'mark@gmail.com', 3],
        ['pete', 'pete@gmail.com', 4]
    ];
    conn.query(sql, { params: values}, function(err) {
        if (err) throw err;
        conn.end();
    });
    

    希望这对其他人有所帮助。

    如果需要,这里是我们如何解决数组插入问题的

    请求来自邮递员(您将查看“来宾”)

    我们是怎么写剧本的

    var express = require('express');
    var utils = require('./custom_utils.js');
    
    module.exports = function(database){
        var router = express.Router();
    
        router.post('/', function(req, res, next) {
            database.query('INSERT INTO activity (author_id, name, date, time, location) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name = VALUES(name), date = VALUES(date), time = VALUES(time), location = VALUES(location)', 
                    [req.body.author_id, req.body.name, req.body.date, req.body.time, req.body.location], function(err, results, fields){
                if(err){
                    console.log(err);
                    res.json({ status: utils.respondMSG.DB_ERROR });
                }
                else {
                    var act_id = results.insertId;
                    database.query('INSERT INTO act_guest (user_id, activity_id, status) VALUES ? ON DUPLICATE KEY UPDATE status = VALUES(status)', 
                            [Array.from(req.body.guests).map(function(g){ return [g, act_id, 0]; })], function(err, results, fields){
                        if(err){
                            console.log(err);
                            res.json({ status: utils.respondMSG.DB_ERROR });
                        }
                        else {
                            res.json({ 
                                status: utils.respondMSG.SUCCEED,
                                data: {
                                    activity_id : act_id
                                }
                            });
                        }
                    });
                }
            });
        });
        return router;
    };
    

    @Ragnar123的答案是正确的,但我看到很多人在评论中说它不起作用。我遇到了同样的问题,您似乎需要像这样将数组包装在
    []
    中:

    var pars = [
        [99, "1984-11-20", 1.1, 2.2, 200], 
        [98, "1984-11-20", 1.1, 2.2, 200], 
        [97, "1984-11-20", 1.1, 2.2, 200]
    ];
    

    它需要像
    [pars]
    一样传递到方法中。

    我在四处寻找批量插入对象的答案

    Ragnar123的回答让我实现了以下功能:

    function bulkInsert(connection, table, objectArray, callback) {
      let keys = Object.keys(objectArray[0]);
      let values = objectArray.map( obj => keys.map( key => obj[key]));
      let sql = 'INSERT INTO ' + table + ' (' + keys.join(',') + ') VALUES ?';
      connection.query(sql, [values], function (error, results, fields) {
        if (error) callback(error);
        callback(null, results);
      });
    }
    
    bulkInsert(connection, 'my_table_of_objects', objectArray, (error, response) => {
      if (error) res.send(error);
      res.json(response);
    });
    

    希望有帮助

    我也有类似的问题。它只是从数组列表中插入一个数组。在做了以下更改后,它工作正常

  • 已将[params]传递给查询方法
  • 将查询从insert(a,b)更改为table1值(?)==>insert(a,b)更改为table1值。删除了围绕问号的偏执 希望这有帮助。我正在使用mysql npm

    我今天(2.16.0)遇到了这个问题,我想与大家分享一下我的解决方案:

    const items = [
        {name: 'alpha', description: 'describes alpha', value: 1},
        ...
    ];
    
    db.query(
        'INSERT INTO my_table (name, description, value) VALUES ?',
        [items.map(item => [item.name, item.description, item.value])],
        (error, results) => {...}
    );
    

    我想提及的几件事是,我正在使用这个包与我的数据库建立连接,您在下面看到的是为insert bulk查询编写的工作代码

    const values = [
      [1, 'DEBUG', 'Something went wrong. I have to debug this.'],
      [2, 'INFO', 'This just information to end user.'],
      [3, 'WARNING', 'Warning are really helping users.'],
      [4, 'SUCCESS', 'If everything works then your request is successful']
    ];
    
    const query = "INSERT INTO logs(id, type, desc) VALUES ?";
    
    const query = connection.query(query, [values], function(err, result) {
      if (err) {
        console.log('err', err)
      }
    
      console.log('result', result)
    });
    
    这是一个快速的“原始拷贝粘贴”,用于在node.js>=11的mysql中推送文件列

    几秒钟内完成250k排

    'use strict';
    
    const mysql = require('promise-mysql');
    const fs = require('fs');
    const readline = require('readline');
    
    async function run() {
      const connection = await mysql.createConnection({
        host: '1.2.3.4',
        port: 3306,
        user: 'my-user',
        password: 'my-psw',
        database: 'my-db',
      });
    
      const rl = readline.createInterface({ input: fs.createReadStream('myfile.txt') });
    
      let total = 0;
      let buff = [];
      for await (const line of rl) {
        buff.push([line]);
        total++;
        if (buff.length % 2000 === 0) {
          await connection.query('INSERT INTO Phone (Number) VALUES ?', [buff]);
          console.log(total);
          buff = [];
        }
      }
    
      if (buff.length > 0) {
        await connection.query('INSERT INTO Phone (Number) VALUES ?', [buff]);
        console.log(total);
      }
    
      console.log('end');
      connection.close();
    }
    
    run().catch(console.log);
    

    可以使用以下代码在Node.js中进行批量插入。为了得到这份工作,我参考了很多博客

    也请参考此链接。

    工作代码

      const educations = request.body.educations;
      let queryParams = [];
      for (let i = 0; i < educations.length; i++) {
        const education = educations[i];
        const userId = education.user_id;
        const from = education.from;
        const to = education.to;
        const instituteName = education.institute_name;
        const city = education.city;
        const country = education.country;
        const certificateType = education.certificate_type;
        const studyField = education.study_field;
        const duration = education.duration;
    
        let param = [
          from,
          to,
          instituteName,
          city,
          country,
          certificateType,
          studyField,
          duration,
          userId,
        ];
    
        queryParams.push(param);
      }
    
      let sql =
        "insert into tbl_name (education_from, education_to, education_institute_name, education_city, education_country, education_certificate_type, education_study_field, education_duration, user_id) VALUES ?";
      let sqlQuery = dbManager.query(sql, [queryParams], function (
        err,
        results,
        fields
      ) {
        let res;
        if (err) {
          console.log(err);
          res = {
            success: false,
            message: "Insertion failed!",
          };
        } else {
          res = {
            success: true,
            id: results.insertId,
            message: "Successfully inserted",
          };
        }
    
        response.send(res);
      });
    
    const educations=request.body.educations;
    设queryParams=[];
    for(设i=0;i

    希望这对您有所帮助。

    如果您想插入对象,请使用以下命令:

        currentLogs = [
     { socket_id: 'Server', message: 'Socketio online', data: 'Port  3333', logged: '2014-05-14 14:41:11' },
     { socket_id: 'Server', message: 'Waiting for Pi to connect...', data: 'Port: 8082', logged: '2014-05-14 14:41:11' }
    ];
    
    console.warn(currentLogs.map(logs=>[ logs.socket_id , logs.message , logs.data , logs.logged ]));
    
    输出将是:

    [
      [ 'Server', 'Socketio online', 'Port  3333', '2014-05-14 14:41:11' ],
      [
        'Server',
        'Waiting for Pi to connect...',
        'Port: 8082',
        '2014-05-14 14:41:11'
      ]
    ]
    

    另外,请查看以了解有关地图功能的更多信息。

    您的问题是什么?您可以使用与使用一个sql命令相同的方法来执行此操作吗?上一个命令完成后,只需启动下一个命令,直到插入所有数据。我的印象是,批量插入比许多单个插入快。在导线级别上,它们是相同的。mysql协议中没有“大容量插入”,mysql中有多个插入,只需使用VALUES关键字即可。使用VALUES语法的INSERT语句可以插入多行。为此,请包含多个列值列表,每个列值用括号括起来,并用逗号分隔。示例:在tbl_名称中插入(a、b、c)值(1,2,3)、(4,5,6)、(7,8,9);这是否提供了与使用准备好的语句执行
    conn.execute()
    相同的保护?如果没有,是否可能
    'use strict';
    
    const mysql = require('promise-mysql');
    const fs = require('fs');
    const readline = require('readline');
    
    async function run() {
      const connection = await mysql.createConnection({
        host: '1.2.3.4',
        port: 3306,
        user: 'my-user',
        password: 'my-psw',
        database: 'my-db',
      });
    
      const rl = readline.createInterface({ input: fs.createReadStream('myfile.txt') });
    
      let total = 0;
      let buff = [];
      for await (const line of rl) {
        buff.push([line]);
        total++;
        if (buff.length % 2000 === 0) {
          await connection.query('INSERT INTO Phone (Number) VALUES ?', [buff]);
          console.log(total);
          buff = [];
        }
      }
    
      if (buff.length > 0) {
        await connection.query('INSERT INTO Phone (Number) VALUES ?', [buff]);
        console.log(total);
      }
    
      console.log('end');
      connection.close();
    }
    
    run().catch(console.log);
    
      const educations = request.body.educations;
      let queryParams = [];
      for (let i = 0; i < educations.length; i++) {
        const education = educations[i];
        const userId = education.user_id;
        const from = education.from;
        const to = education.to;
        const instituteName = education.institute_name;
        const city = education.city;
        const country = education.country;
        const certificateType = education.certificate_type;
        const studyField = education.study_field;
        const duration = education.duration;
    
        let param = [
          from,
          to,
          instituteName,
          city,
          country,
          certificateType,
          studyField,
          duration,
          userId,
        ];
    
        queryParams.push(param);
      }
    
      let sql =
        "insert into tbl_name (education_from, education_to, education_institute_name, education_city, education_country, education_certificate_type, education_study_field, education_duration, user_id) VALUES ?";
      let sqlQuery = dbManager.query(sql, [queryParams], function (
        err,
        results,
        fields
      ) {
        let res;
        if (err) {
          console.log(err);
          res = {
            success: false,
            message: "Insertion failed!",
          };
        } else {
          res = {
            success: true,
            id: results.insertId,
            message: "Successfully inserted",
          };
        }
    
        response.send(res);
      });
    
        currentLogs = [
     { socket_id: 'Server', message: 'Socketio online', data: 'Port  3333', logged: '2014-05-14 14:41:11' },
     { socket_id: 'Server', message: 'Waiting for Pi to connect...', data: 'Port: 8082', logged: '2014-05-14 14:41:11' }
    ];
    
    console.warn(currentLogs.map(logs=>[ logs.socket_id , logs.message , logs.data , logs.logged ]));
    
    [
      [ 'Server', 'Socketio online', 'Port  3333', '2014-05-14 14:41:11' ],
      [
        'Server',
        'Waiting for Pi to connect...',
        'Port: 8082',
        '2014-05-14 14:41:11'
      ]
    ]