我如何使用node.js在MySQL中进行批量更新

编程入门 行业动态 更新时间:2024-10-28 12:28:21
本文介绍了我如何使用node.js在MySQL中进行批量更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想批量更新数据,我有50多个行要在节点JS中的对象数组中更新.就像是 github/felixge/node-mysql 和我如何使用node.js在MySQL中进行批量插入

i want to update data in bulk i have over 50 rows to be updated in an array of objects in node JS. something like github/felixge/node-mysql and How do I do a bulk insert in mySQL using node.js

var updateData=[ {a: '15',b: 1,c: '24',d: 9,e: 1,f: 0,g: 0,h: 5850,i: 78 }, {a: '12',b: 1,c: '21',d: 9,e: 1,f: 0,g: 0,h: 55,i: 78 }, {a: '13',b: 1,c: '34',d: 9,e: 1,f: 0,g: 0,h: 58,i: 78 }, {a: '14',b: 1,c: '45',d: 9,e: 1,f: 0,g: 0,h: 585,i:78 }, {a: '16',b: 1,c: '49',d: 9,e: 1,f: 0,g: 0,h: 85,i: 78 } ] my query is : update table set a= updateData.a ,b= updateData.b ,c = updateData.c , d==updateData.d ,e=updateData.e,f=updateData.f where e=updateData.e

推荐答案

据我所知,没有直接方法可以在mySQL中进行批量更新记录.但是,有一种解决方法-您可以执行多个insert语句,然后执行查询以获得所需的结果.

As of I know, there is no direct way to do bulk update records in mySQL. But there is a work around for this - You could execute multiple insert statements and then execute the query to achieve the desired result.

为此,在创建连接时允许它执行多个语句,因为默认情况下它是禁用的.

To do this, while creating a connection allow it to execute multiple statements as it is disabled by default.

var connection = mysql.createConnection({ host : dbConfig.host, user : dbConfig.user, password : dbConfig.password, database : dbConfig.database, multipleStatements: true });

然后,通过操纵您的输入,以以下语法构造批量更新查询.

Then, construct the bulk update query in the below syntax by manipulating the inputs you have.

Query1; Query2; Query3;

Query1; Query2; Query3;

例如,

update table set a='15', b=1, c='24', d=9, e=1, f=0, g=0, h=5850, i=78;update table set a='12', b=1, c='21', d=9, e=1, f=0, g=0, h=5850, i=78;

然后,像往常一样执行查询,

Then, execute the query as usual,

connection.query(sqlQuery, params, callback);

希望这会有所帮助.

更多推荐

我如何使用node.js在MySQL中进行批量更新

本文发布于:2023-10-23 22:20:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   批量   js   node   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!