最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 2浏览 0评论

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

我想要批量更新数据我有超过50行要在节点JS中的对象数组中更新。类似于 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中进行批量更新记录。但是有一个解决方法 - 您可以执行多个插入语句,然后执行查询以实现所需的结果。

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

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

然后,通过操作您拥有的输入,使用以下语法构造批量更新查询。

查询1; 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;

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

connection.query(sqlQuery, params, callback);

希望这可以帮助。

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

我想要批量更新数据我有超过50行要在节点JS中的对象数组中更新。类似于 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中进行批量更新记录。但是有一个解决方法 - 您可以执行多个插入语句,然后执行查询以实现所需的结果。

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

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

然后,通过操作您拥有的输入,使用以下语法构造批量更新查询。

查询1; 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;

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

connection.query(sqlQuery, params, callback);

希望这可以帮助。

发布评论

评论列表 (0)

  1. 暂无评论