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
Related
I am trying to dynamically build a mysql statement but I keep getting incorrect syntax.
const tableName = user_data;
const insertSet = {'id': 1, 'dependents': ['sarah', 'john']}
conn.query('insert into ?? set ?', [tableName, insertSet], (err,rows) => {
if (err) reject(err);
resolve(rows);
}
When I try this I get the following error:
You have an error in your SQL syntax
In the error message, I saw that the sql it tried to run is
insert into user_data set id = 1, dependents='sarah','john';
It took away the array from dependents and this caused the syntax error.
It should be
insert into user_data set id = 1, dependents=[ 'sarah','john' ];
I am using aws as my backend and i have few aws lambda functions (written in node JS) that are used to insert incoming json data to amazon RDS(mysql) DB. Below is my node js code
var mysql = require('mysql');
var config = require('./config.json');
var pool = mysql.createPool({.../});
exports.handler = (event, context, callback) => {
let inserts = [event.unitID, event.timestamp, event.frequency];
pool.getConnection(function(error, connection) {
connection.query({
sql: 'INSERT INTO device_data (device_id, timestamp, frequency) VALUES (?, ?, ?);',
timeout: 40000,
values: inserts
}, function(error, results, fields) {
connection.release();
if (error) callback(error);
else callback(null, results);
});
});
};
This is the incoming json data
"unitID": "arena-MXHGMYzBBP5F6jztnLUdCL",
"timestamp": 1580915318000,
"version": "1.0.0",
"frequency": [
60.0033,
60.004,
60.0044,
60.0032,
60.005,
60.005,
60.0026,
60.0035,
60.0036,
60.0053
]
}
my frequency has array of values and i am unable to handle that to insert into DB.
Any suggestions. Thanks
if your data is in a variable called json:
console.log(json.frequency.map( (freq) =>[json.unitID,json.timestamp,freq] ))
you can then tweak this to fit your sql to a string that replaces VALUES (?,?,?) with your desired output. e.g.:
const values = json.frequency.map( (freq) => [json.unitID,json.timestamp,freq] );
const sqlString = `'INSERT INTO device_data (device_id, timestamp, frequency) VALUES ${values.map( (row) => `(${row[0]},${row[1]},${row[2]})` ).join(',')}`
and in your code:
connection.query({
sql:sqlString
[...]
I need some help, When i make this query in workbench its work fine
INSERT INTO reports(creatorUserID,currentUserEditorID,companyID,stageID,scenarioID,typeID,year) VALUES (1,1,456545,1,1,1,1500);
INSERT INTO reports_tax_data(reportID,data) VALUES (LAST_INSERT_ID(),'work perfect');
but when i do the same things in nodejs
const createNewReport = () => {
return new Promise((resolve, reject) => {
connection.query(
`INSERT INTO reports(creatorUserID,currentUserEditorID,companyID,stageID,scenarioID,typeID,year) VALUES (1,1,456545,1,1,1,1500);
INSERT INTO reports_tax_data(reportID,data) VALUES (LAST_INSERT_ID(),'work perfect');`,
(err, result) => {
if (err) reject(err);
resolve(result);
}
);
});
};
i get this error:
sqlMessage:
'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'INSERT INTO reports_tax_data(reportID,data) VALUES (LAST_INSERT_ID(),\'work perfe\' at line 2',
sqlState: '42000',
index: 0,
sql:
'INSERT INTO reports(creatorUserID,currentUserEditorID,companyID,stageID,scenarioID,typeID,year) VALUES (1,1,456545,1,1,1,1500);\n INSERT INTO reports_tax_data(reportID,data) VALUES (LAST_INSERT_ID(),\'work perfect\');' }
But when i make a normal select its work in nodejs
I would assume here that the Node SQL API you are using does not allow more than one statement to be executed per call (this is certainly the case for a few other programming languages). Try making separate calls for each insert:
connection.query(
`INSERT INTO reports(creatorUserID,currentUserEditorID,companyID,stageID,scenarioID,typeID,year) VALUES (1,1,456545,1,1,1,1500)`,
(err, result) => {
if (err) reject(err);
resolve(result);
}
);
And do the same for the reports_tax_data table insert.
i think you have to set a below mention configuration to allow multiple queries on node js.
var connection = mysql.createConnection({multipleStatements: true});
please check and comment if any issue.
The problem I'm having is with passing the object from Node.js to the database (MySQL).
currently, I'm using the INSERT method which only allows me to pass strings, if I try to pass an object I get an error
Here is what I'm using right now :
let new_movie_data = `INSERT INTO testing (name, last) VALUES ('Jhonee','Mel')`
connection.query(new_movie_data, function (err, result){
if(err) throw (err)
})
This is what I get from react and want to send to the database :
{
name:"Jhonee",
last:"Mel"
}
trying to put this in the "VALUES" will cause an error.
Thanks :D
You could parse the JSON in to its components:
jsonObj = JSON.parse(stringFromReact);
const new_movie_data = 'INSERT INTO testing (name, last) VALUES (?, ?);
connection.query(new_movie_data, [jsonObj.name, jsonObj.last], function (err, result){
if(err) throw (err)
});
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