我正在使用express和node-pg将excel文件导入postgres数据库
目前我循环遍历excel行并为每一行执行插入,但我觉得这不是正确的方法:
workbook.xlsx.readFile(excel_file).then(function () { // get the first worksheet var worksheet = workbook.getWorksheet(1); // Loop through all rows worksheet.eachRow(function (row,rowNumber) { // Commit to DB only from line 2 and up. We want to exclude headers from excel file if (rowNumber > 1) { // Loop through all values and build array to pass to DB function row.eachCell(function (cell,colNumber) { arrsqlParams.push(cell.value) }) // Add the user id from session to the array arrsqlParams.push(user); // Insert into DB db.query(strsql,arrsqlParams,function (err,result) { if (err) { console.log(err); ret = false; } }) // Empty the array for new query arrsqlParams = []; } }) });
解决方法
在作者提供澄清之后,为了一次最多插入1000条记录,Multi-row insert with pg-promise中建议的解决方案正是作者在性能和灵活性方面所需要的.
UPDATE
必读文章:Data Imports.