How to insert batch data into MySQL database under Node.js

How to insert batch data into MySQL database under Node.js

In the project (nodejs), multiple data need to be inserted into the database at one time. The database is MySQL. Since the performance of loop insertion is too poor, it is like using batch insertion to improve the data insertion performance.

The table structure of the database for batch insertion is as follows:

這里寫圖片描述

1. Database connection

var mysql = require('mysql');
// Database information var connection = mysql.createConnection({
 host : 'localhost',
 user: 'Database user name',
 password: 'Database login password',
 database: 'Operation database name'
});

Convert the inserted data into a nested array

For example, two pieces of data to be inserted:

Record 1:

from:"index" 
to: "www.alibaba.com"
status:1
is_new:0

Record 2:

from:"index1"
to:"www.google.com"
status:1
is_new:0

Convert to the following format:

var values ​​= [
 ["index","www.alibaba.com",1,0],
 ["index1","www.google.com",1,0]
];

Write an insert statement

var sql = "INSERT INTO url(`from`,`to`,`status`, `is_new`) VALUES ?";

Call the query function to complete the data insertion

connection.query(sql, [values], function (err, rows, fields) {
 if(err){
    console.log('INSERT ERROR - ', err.message);
    return;
   }
   console.log("INSERT SUCCESS");
});

Full code:

var mysql = require('mysql');
// Database information var connection = mysql.createConnection({
 host : 'localhost',
 user: 'Database user name',
 password: 'Database login password',
 database: 'Operation database name'
});
var values ​​= [
 ["index","www.alibaba.com",1,0],
 ["index1","www.google.com",1,0]
];
var sql = "INSERT INTO url(`from`,`to`,`status`, `is_new`) VALUES ?";
connection.query(sql, [values], function (err, rows, fields) {
 if(err){
    console.log('INSERT ERROR - ', err.message);
    return;
   }
   console.log("INSERT SUCCESS");
});

At the same time, record a transaction-based operation here (no practice yet, the specific effect is unknown)

Use transaction loop to insert and roll back if one insert fails

這里寫圖片描述

mysql module, connection.beginTransaction is to do transactions

Then I encapsulate a function here to perform operations such as loop insertion or update on the passed array. If one fails, roll back, and commit if all are correct.

Summarize

The above is what I introduced to you about inserting batch data into MySQL database under Node.js. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Example of connecting to MySQL database using Node.js
  • MySQL database configuration and connection under node.js platform
  • Node.js database operation: querying MySQL database (Part 2)
  • Node.js database operation: connecting to MySQL database (I)
  • Learning node.js mysql database operations from scratch (V)
  • Node.js operates mysql database to add, delete, modify and query
  • How to configure MySQL or Oracle database for Node.js program under Linux
  • Node.js Development Guide – Node.js connects to MySQL and performs database operations
  • How to operate MySQL database with node.js

<<:  How to prevent website content from being included in search engines

>>:  Example of adding multi-language function to Vue background management

Recommend

Docker exec executes multiple commands

The docker exec command can execute commands in a...

MySQL 8.0.20 installation and configuration method graphic tutorial

MySQL download and installation (version 8.0.20) ...

Detailed steps for building Portainer visual interface with Docker

In order to solve the problem mentioned last time...

Detailed explanation of pipeline and valve in tomcat pipeline mode

Preface In a relatively complex large system, if ...

A brief analysis of the usage of USING and HAVING in MySQL

This article uses examples to illustrate the usag...

Build a Docker image using Dockerfile

Table of contents Build a Docker image using Dock...

How to configure MySQL on Ubuntu 16.04 server and enable remote connection

background I am learning nodejs recently, and I r...

Solution to Docker pull timeout

Recently, Docker image pull is very unstable. It ...

What are the benefits of using B+ tree as index structure in MySQL?

Preface In MySQL, both Innodb and MyIsam use B+ t...

Simple operation of installing vi command in docker container

When using a docker container, sometimes vim is n...

Solution to the long delay of MySQL database master-slave replication

Preface The delay of MySQL master-slave replicati...

MySQL 5.7.23 installation and configuration graphic tutorial

This article records the detailed installation pr...