insert multiple rows into mysql through node.js - mysql

I want to insert multiple rows into mysql thru node.js mysql module. The data I have is
var data = [{'test':'test1'},{'test':'test2'}];
I am using pool
pool.getConnection(function(err, connection) {
connection.query('INSERT INTO '+TABLE+' SET ?', data, function(err, result) {
if (err) throw err;
else {
console.log('successfully added to DB');
connection.release();
}
});
});
}
which fails.
Is there a way for me to have a bulk insertion and call a function when all insertion finishes?
Regards
Hammer

After coming back to this issue multiple times, I think i've found the cleanest way to work around this.
You can split the data Array of objects into a set of keys insert_columns and an array of arrays insert_data containing the object values.
const data = [
{test: 'test1', value: 12},
{test: 'test2', value: 49}
]
const insert_columns = Object.keys(data[0]);
// returns array ['test', 'value']
const insert_data = data.reduce((a, i) => [...a, Object.values(i)], []);
// returns array [['test1', 12], ['test2', 49]]
_db.query('INSERT INTO table (??) VALUES ?', [insert_columns, insert_data], (error, data) => {
// runs query "INSERT INTO table (`test`, `value`) VALUES ('test1', 12), ('test2', 49)"
// insert complete
})
I hope this helps anyone coming across this issues, I'll probably be googling this again in a few months to find my own answer 🤣

You can try this approach as well
lets say that mytable includes the following columns: name, email
var inserts = [];
inserts.push(['name1', 'email1']);
inserts.push(['name2', 'email2']);
conn.query({
sql: 'INSERT into mytable (name, email) VALUES ?',
values: [inserts]
});
This should work

You can insert multiple rows into mysql using nested arrays. You can see the answer from this post: How do I do a bulk insert in mySQL using node.js

Related

Bulk insert in mySQL using node.js not working

Referring to this thread How do I do a bulk insert in mySQL using node.js I have written the below code
const values = [
['e33b3c34923500e6', 'The Office', '330248b39a55d82a', '690db8528741c098'],
['e33b3c34923500e7', 'Prison Break', '330248b39a55d82a', '690db8528741c098']
]
let sql = `INSERT INTO Result (resultId, result, submissionId, questionId) VALUES ?`
connection.query(sql, [values], (err, res) => {console.log(err)
console.log(res)
})
Normal single inserts work with the given values but whenever trying to bulk insert like above I get the following error:
return await usedQueryRunner.query(query, parameters); // await is needed here because we are using finally TypeError: usedQueryRunner.query is not a function

Insert array to MySQL in node.js?

How can I insert an array to a table in MySQL with nodejs? The code below works if the array is just one, but if there is more items in the array, I get the error: "ER_WRONG_VALUE_COUNT_ON_ROW"
I need to insert all the array values in the table. How can I achieve this?
Here is what I got so far:
let workoutArray = [];
result.forEach(function(name) {
workoutArray.push(name.exercise);
});
let sql2 = 'INSERT INTO reps (email, date, workout, exercise) VALUES (?,?,?,?)';
connection.query(sql2, [user, addDate, workoutToIndex, workoutArray], function (error2, result2) {
if (error2) throw error2;
console.log(result2);
});
response.end();
});
Instead of sending an array as a parameter, try sending an array from an array.
Example:
let workoutArray = [];
result.forEach(function(name) {
workoutArray.push(name.exercise);
});
let sql2 = 'INSERT INTO reps (email, date, workout, exercise) VALUES (?,?,?,?)';
connection.query(sql2, [[user, addDate, workoutToIndex, workoutArray]], function (error2, result2) {
if (error2) throw error2;
console.log(result2);
});
response.end();
});

Is there any way to dynamically set multiple VALUES in mysql 'insert' query in nodejs?

I am working on nodejs for the first time. I have a scenario where I am having json array like shown below.
I will not know how many json items will be there in that array.
I have tried solution using loops but it will fire the query for multiple times. And I don't want that.
{"qualification":[{"degreeName":"B","domain":"p"},{"degreeName":"A","domain":"q"}]}
And mysql query will be like this
INSERT INTO qualification (degreeName,domain) VALUES (B,p),(A,q);
In the above query I have explicitly written values, but as I will not know how many values will be there I can't write values like that, instead i will have to put all values in a variable and then pass it to the query.
So how can I retrive and convert qualification data into varible or tuples to put into mysql query so that I can fire single query and add multiple values.
You could do something like this: You'll have to generate values array from dynamic values and pass onto the query.
Key point: ? is a unnamed parameter (placeholder alias) to make it Parameterised Query
const mysql = require('mysql');
const con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
let sql = "INSERT INTO qualification (degreeName, domain) VALUES ?";
let values = [
['B', 'p'],
['A', 'q'],
...
]; //this is dynamic value that you can create
con.query(sql, [values], function (err, result) {
if (err) throw err;
console.log("Number of records inserted: " + result.affectedRows);
});
});
To generate the tuples (values) from the dataset (assumes that data is going be in that format):
const payload = {
qualification: [
{ degreeName: "B", domain: "p" },
{ degreeName: "A", domain: "q" }
]
};
const tuples = payload.qualification.map(obj => [obj.degreeName, obj.domain]);
Which OPs something like this:
[ [ 'B', 'p' ], [ 'A', 'q' ] ]

insert multiple rows having 2 columns in nodejs

I have 2 arrays in node js code
names = ['Name1','Name2','Name3','Name4', ...500 more items]
hashes = ['hash1','hash2','hash3','hash4', ...500 more items]
I have 2 columns in database table namely as 'Name' and 'hash'. I want to insert name and hash values in multiple rows simultaneously using only one mysql statement.
I tried to do it with one array. It executed successfully but its not working with 2 arrays. How should i do it ?
The Mysql insert query i wrote for one array is shown below:
var sql = "Insert IGNORE into lu (Name) VALUES ?";
con.query(sql,[array1],function(err, result){
if (err){
con.rollback(function(){
throw err;
});
}
});
You can just map names and hashes into one array - [["Name1", "hash1"], ...], then insert a nested array of elements.
var sql = "INSERT IGNORE INTO lu(Name, hash) VALUES ?";
var names = ['Name1','Name2','Name3','Name4'];
var hashes = ['hash1','hash2','hash3','hash4'];
var toOneArray = function(names, hashes) {
return names.map(function(name, i) {
return [name, hashes[i]]
});
}
con.query(sql, [toOneArray(names, hashes)], function(err, result) {
if (err) {
con.rollback(function(){
throw err;
});
}
});
I do not know if there is another way.

Node.js - mysql: Bad field Error

I parse a big csv and insert row per row into my mysql tables.
After parsing I do a lot of calculation and transforming and save it to a new Object
obj.push({
"ID": value.id,
"col1": value.calc1,
... });
After the Object is complete I do:
async.forEach(obj, function (Insertobj, callback) {
var query = conn.query('INSERT INTO table SET ?', Insertobj);
},function (err){
if (err) {
console.log(err);
console.log('failed to process');
}}
After running through the obj I get =>
Error: ER_BAD_FIELD_ERROR: Unknown column 'NaN' in 'field list'..
But he inserts the complete Object into my table! I don't have any column called NaN or empty cols. How can I debug it? I try to set the console.log to err.sql, but he print "undefined". Using debug:true in connection didn't help me.
I think you have misunderstood how escaping mysql values works using the node js module. The error is due to you not specifying what column you want to update. In addition to this, the escaped values should be filled in using an array instead of an object. With values being in the order they are escaped in the query. Your code could look as follows:
valuesarray.push([
value.id,
value.calc1
]);
async.forEach(valuesarray, function ( insertarray, callback ) {
var query = conn.query('INSERT INTO table SET ID = ?, col1 =
?', insertarray);
},function (err){
if (err) {
console.log(err);
console.log('failed to process');
}
});