Home >Database >Mysql Tutorial >How Can I Perform Bulk Inserts into MySQL Using Node-MySQL?

How Can I Perform Bulk Inserts into MySQL Using Node-MySQL?

Linda Hamilton
Linda HamiltonOriginal
2024-12-12 13:15:111024browse

How Can I Perform Bulk Inserts into MySQL Using Node-MySQL?

Performing Bulk Inserts in MySQL Using Node.js and Node-MySQL

When utilizing the node-mysql library (https://github.com/felixge/node-mysql) to interact with MySQL, it is possible to perform bulk inserts to improve efficiency.

To achieve bulk inserts, you can employ nested arrays. The node-mysql documentation states that "nested arrays are turned into grouped lists (for bulk inserts)."

To clarify, you insert nested arrays of elements. For instance, consider the following example:

var mysql = require('mysql');
var conn = mysql.createConnection({
    ...
});

var sql = "INSERT INTO Test (name, email, n) VALUES ?";
var values = [
    ['demian', '[email protected]', 1],
    ['john', '[email protected]', 2],
    ['mark', '[email protected]', 3],
    ['pete', '[email protected]', 4]
];
conn.query(sql, [values], function(err) {
    if (err) throw err;
    conn.end();
});

Here, values is an array of arrays, where each inner array represents a row to be inserted.

It is important to note that you can also utilize a different node-msql package that is specifically designed for bulk inserts.

The above is the detailed content of How Can I Perform Bulk Inserts into MySQL Using Node-MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn