How do I do a bulk insert in mySQL using node.js

后端 未结 12 2296
一整个雨季
一整个雨季 2020-11-22 10:39

How would one do a bulk insert into mySQL if using something like https://github.com/felixge/node-mysql

12条回答
  •  逝去的感伤
    2020-11-22 10:52

    This is a fast "raw-copy-paste" snipped to push a file column in mysql with node.js >= 11

    250k row in few seconds

    '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);
    

提交回复
热议问题