执行脚本
const fs = require("fs"); const csv = require("csv-parser"); const iconv = require('iconv-lite'); const results = []; const sqlStatements = []; fs.createReadStream("./ICD10.csv") .pipe(iconv.decodeStream('UTF-8')) // Specify the correct encoding used in the CSV file .pipe(csv()) .on("data", (data) => results.push(data)) .on("end", () => { results.forEach((row,index) => { const sql = `INSERT INTO icd10 (name, code, name2, code2) VALUES ('${row.name}', '${row.code}', '${row.name2}' , '${row.code2}');`; sqlStatements.push(sql); }); fs.writeFileSync("ICD10.sql", sqlStatements.join("\n")); });
csv内容
code name code2 name2 A00.000 霍乱,由于O1群霍乱弧菌,霍乱生物型所致 A00.000 霍乱,由于O1群霍乱弧菌,霍乱生物型所致 A00.000x001 古典生物型霍乱 A00.000x001 古典生物型霍乱 A00.100 霍乱,由于O1群霍乱弧菌,埃尔托生物型所致 A00.100 霍乱,由于O1群霍乱弧菌,埃尔托生物型所致 A00.100x001 埃尔托生物型霍乱 A00.100x001 埃尔托生物型霍乱
生成sql语句
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; INSERT INTO icd10 (name, code, name2, code2) VALUES ('霍乱,由于O1群霍乱弧菌,霍乱生物型所致', 'A00.000', '霍乱,由于O1群霍乱弧菌,霍乱生物型所致' , 'A00.000'); INSERT INTO icd10 (name, code, name2, code2) VALUES ('古典生物型霍乱', 'A00.000x001', '古典生物型霍乱' , 'A00.000x001'); INSERT INTO icd10 (name, code, name2, code2) VALUES ('霍乱,由于O1群霍乱弧菌,埃尔托生物型所致', 'A00.100', '霍乱,由于O1群霍乱弧菌,埃尔托生物型所致' , 'A00.100'); INSERT INTO icd10 (name, code, name2, code2) VALUES ('埃尔托生物型霍乱', 'A00.100x001', '埃尔托生物型霍乱' , 'A00.100x001'); INSERT INTO icd10 (name, code, name2, code2) VALUES ('霍乱', 'A00.900', '霍乱' , 'A00.900');
格式问题
需要设置编码格式,不然中文会乱码
SET NAMES utf8mb4