This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-16. For the latest, interactive version please visit https://mariadb.com/kb/.

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

Can someone help me with inserting multiple rows using mariadb, checked on stackoverflow with no solution in my case. I'm able to insert single rows fine, but when it needs to be 2 rows, I get a syntax error. I have tried putting the values parameter in brackets with no success.

Here's my code:

const config = require('../config');
const mariadb  = require('mariadb');

const pool = mariadb.createPool({
	host: config.database.host,
	user: config.database.user,
	port: config.database.port,
	password: config.database.password,
	database: config.database.database,
	multipleStatements: true,
	connectionLimit: 5
});
app.post('/questions', (req, res) => {

let values = [];

for (var i = 0; i < multiAnswerId.length; i++) {
values.push(new Array(quiz_id, question_id, multiAnswerId[i], multiCorrect[i], time_start, userPicks.time_stop, student_id));
		}
var sql = `INSERT INTO Govt_choice_response (Quiz_id, Question_id, Answer_id, Correct, Time_start, Time_stop, Student_id)
		VALUES ?`;

		pool.query(sql, values, (err, data) => {
		})
		.then( rows => {
			if (question_id > qTotal) {
				res.send(`
					<h1>You finished the test! (Make a page later here with maybe some user data)</h1>
				`)
			}
			question_id ++;
			res.redirect('/questions');
		})	
		.catch(err => {
			console.log(err);
		})
     });
}
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.