javascript - How do I insert multiple records from array in mySQL using node.js - Stack Overflow

I have a look-up table:CREATE TABLE technologies (technologyName VARCHAR(50) NOT NULL PRIMARY KEY);I w

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
Add a ment  | 

2 Answers 2

Reset to default 2

if 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745499086a4630330.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信