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

后端 未结 12 2473
一整个雨季
一整个雨季 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:53

    Few things I want to mention is that I'm using mysql package for making a connection with my database and what you saw below is working code and written for insert bulk query.

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

提交回复
热议问题