How do I do a bulk insert in mySQL using node.js

后端 未结 12 2283
一整个雨季
一整个雨季 2020-11-22 10:39

How would one do a bulk insert into mySQL if using something like https://github.com/felixge/node-mysql

12条回答
  •  北海茫月
    2020-11-22 11:12

    I ran into this today (mysql 2.16.0) and thought I'd share my solution:

    const items = [
        {name: 'alpha', description: 'describes alpha', value: 1},
        ...
    ];
    
    db.query(
        'INSERT INTO my_table (name, description, value) VALUES ?',
        [items.map(item => [item.name, item.description, item.value])],
        (error, results) => {...}
    );
    

提交回复
热议问题