Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

node-mysql 插入多条数据

我有100条数据,怎么用node插入到mysql在中啊,

我找了一种这样的但是不好

 var query = 'insert into news set ?';  
 connection.query(query,{   
     title : 'node',   
     content : 'node',   
     nameConpany : 'woman',
     time :'1512151'
}

我的数据结构是这样{{“title ”,“content ”,“nameConpany ”,“time ”},{“title ”,“content ”,“nameConpany ”,“time ”},......}求怎么做


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

用事务循环插入、如果有一条插入失败进行回滚

clipboard.png

mysql模块、connection.beginTransaction是做事务

然后我这里封装了一个函数、对传入的数组做循环插入或更新之类的操作、如果有一条失败了就回滚、全对了就commit


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...