Code Example: Batching DML
const mariadb = require("mariadb");
// Main function
async function main() {
let conn;
try {
conn = await mariadb.createConnection({
host: "192.0.2.50",
user: "db_user",
password: "db_user_password",
database: "test",
});
//Declare a JSON array for data to add
var contacts = [
["John", "Smith", "john.smith@example.com"],
["Jon", "Smith", "jon.smith@example.com"],
["Johnny", "Smith", "johnny.smith@example.com"],
];
// Use Connection to Add Contacts in Batch
await add_contacts(conn, contacts);
} catch (err) {
// Manage Errors
console.log(err);
} finally {
// Close Connection
if (conn) conn.close();
}
}
async function add_contacts(conn, data) {
return conn.batch(
"INSERT INTO test.contacts(first_name, last_name, email) VALUES (?, ?, ?)",
data
);
}
main();
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以邮件至 kimfei2014@gmail.com