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

Tutorial analysis of quick installation of mysql5.7 based on centos7

one. wget https://dev.mysql.com/get/mysql57-commu...

MySQL Optimization: Cache Optimization

I am happy that some bloggers marked my article. ...

WeChat applet implements search box function

This article example shares the specific code for...

The latest version of MySQL5.7.19 decompression version installation guide

MySQL version: MySQL Community Edition (GPL) ----...

Analysis of the event loop mechanism of js

Preface As we all know, JavaScript is single-thre...

Six ways to reduce the size of Docker images

Since I started working on Vulhub in 2017, I have...

18 common commands in MySQL command line

In daily website maintenance and management, a lo...

Several ways to backup MySql database

mysqldump tool backup Back up the entire database...

A brief discussion on the problem of forgotten mysql password and login error

If you forget your MySQL login password, the solu...

A "classic" pitfall of MySQL UPDATE statement

Table of contents 1. Problematic SQL statements S...

Vue3.0+vite2 implements dynamic asynchronous component lazy loading

Table of contents Create a Vite project Creating ...

How to install PostgreSQL and PostGIS using yum on CentOS7

1. Update the yum source The PostgreSQL version o...

Solution to Docker disk space cleaning

Some time ago, I encountered the problem that the...

Solve the error of installing VMware Tools on Ubuntu 18.04

1. According to the online tutorial, the installa...

Example of how to adapt the Vue project to the large screen

A brief analysis of rem First of all, rem is a CS...