admin管理员组文章数量:1024924
I have a look-up table:
CREATE TABLE technologies (
technologyName VARCHAR(50) NOT NULL PRIMARY KEY
);
I want to store a new record in this table for every element in an array:
var values = ['Grunt', 'Gulp'];
So that the resulting table will look like:
+----------------+
| technologyName |
+----------------+
| Grunt |
+----------------+
| Gulp |
+----------------+
How can I do this using Node?
Update:
I know I can do something like this:
var values = [
[
['Grunt'],
['Something']
]
]
connection.query('INSERT IGNORE INTO technologies (technologyName) VALUES ?', values);
I tried this with the original array but it does not work. How can I convert the simple array into the more plicated one?
I have a look-up table:
CREATE TABLE technologies (
technologyName VARCHAR(50) NOT NULL PRIMARY KEY
);
I want to store a new record in this table for every element in an array:
var values = ['Grunt', 'Gulp'];
So that the resulting table will look like:
+----------------+
| technologyName |
+----------------+
| Grunt |
+----------------+
| Gulp |
+----------------+
How can I do this using Node?
Update:
I know I can do something like this:
var values = [
[
['Grunt'],
['Something']
]
]
connection.query('INSERT IGNORE INTO technologies (technologyName) VALUES ?', values);
I tried this with the original array but it does not work. How can I convert the simple array into the more plicated one?
Share Improve this question edited Apr 24, 2015 at 14:52 Angular noob asked Apr 24, 2015 at 14:24 Angular noobAngular noob 4375 silver badges11 bronze badges 2- @MelissaAvery-Weir No, I do not need do do that. I made the choice to keep the question short and sweet for anyone who might stumble upon it in the future. – Angular noob Commented Apr 24, 2015 at 14:48
- Nope, you certainly don't need to (i.e., the question is unlikely to be closed). I should have said "your question will be more highly voted and more readily answered if you include what you've tried" (per stackoverflow./help/how-to-ask). That all said, thanks for adding the example. – Melissa Avery-Weir Commented Apr 24, 2015 at 14:58
2 Answers
Reset to default 2if you want to use "?" to escape your values and avoid SQL injections, mysqljs accept nested arrays for bulk inserts. So the answer to your update is :
var valuesNestedArray = [];
for(var v in values ) {
valuesNestedArray.push([values[v]]);
}
var sql = "INSERT IGNORE INTO technologies (technologyname) values ?";
connection.query(sql, [valuesNestedArray], function(err) {
if (err) throw err;
});
You can insert multiple things at once...
From the mysql docs (https://dev.mysql./doc/refman/5.5/en/insert.html)
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
so for you:
insert into technologies (technologyname) values ('grunt'),('gulp')
var sql = "insert into technologies (technologyname) values ";
for(var v in values)
sql += "('" + connection.escape(values[v]) + "'),";
sql = sql.substr(0,sql.length-1); // take off the last ma since we added one after each value
I have a look-up table:
CREATE TABLE technologies (
technologyName VARCHAR(50) NOT NULL PRIMARY KEY
);
I want to store a new record in this table for every element in an array:
var values = ['Grunt', 'Gulp'];
So that the resulting table will look like:
+----------------+
| technologyName |
+----------------+
| Grunt |
+----------------+
| Gulp |
+----------------+
How can I do this using Node?
Update:
I know I can do something like this:
var values = [
[
['Grunt'],
['Something']
]
]
connection.query('INSERT IGNORE INTO technologies (technologyName) VALUES ?', values);
I tried this with the original array but it does not work. How can I convert the simple array into the more plicated one?
I have a look-up table:
CREATE TABLE technologies (
technologyName VARCHAR(50) NOT NULL PRIMARY KEY
);
I want to store a new record in this table for every element in an array:
var values = ['Grunt', 'Gulp'];
So that the resulting table will look like:
+----------------+
| technologyName |
+----------------+
| Grunt |
+----------------+
| Gulp |
+----------------+
How can I do this using Node?
Update:
I know I can do something like this:
var values = [
[
['Grunt'],
['Something']
]
]
connection.query('INSERT IGNORE INTO technologies (technologyName) VALUES ?', values);
I tried this with the original array but it does not work. How can I convert the simple array into the more plicated one?
Share Improve this question edited Apr 24, 2015 at 14:52 Angular noob asked Apr 24, 2015 at 14:24 Angular noobAngular noob 4375 silver badges11 bronze badges 2- @MelissaAvery-Weir No, I do not need do do that. I made the choice to keep the question short and sweet for anyone who might stumble upon it in the future. – Angular noob Commented Apr 24, 2015 at 14:48
- Nope, you certainly don't need to (i.e., the question is unlikely to be closed). I should have said "your question will be more highly voted and more readily answered if you include what you've tried" (per stackoverflow./help/how-to-ask). That all said, thanks for adding the example. – Melissa Avery-Weir Commented Apr 24, 2015 at 14:58
2 Answers
Reset to default 2if you want to use "?" to escape your values and avoid SQL injections, mysqljs accept nested arrays for bulk inserts. So the answer to your update is :
var valuesNestedArray = [];
for(var v in values ) {
valuesNestedArray.push([values[v]]);
}
var sql = "INSERT IGNORE INTO technologies (technologyname) values ?";
connection.query(sql, [valuesNestedArray], function(err) {
if (err) throw err;
});
You can insert multiple things at once...
From the mysql docs (https://dev.mysql./doc/refman/5.5/en/insert.html)
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
so for you:
insert into technologies (technologyname) values ('grunt'),('gulp')
var sql = "insert into technologies (technologyname) values ";
for(var v in values)
sql += "('" + connection.escape(values[v]) + "'),";
sql = sql.substr(0,sql.length-1); // take off the last ma since we added one after each value
本文标签: javascriptHow do I insert multiple records from array in mySQL using nodejsStack Overflow
版权声明:本文标题:javascript - How do I insert multiple records from array in mySQL using node.js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745499084a2153306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论