将数据库以CSV格式从latin1编码的string导出到utf-8

我们有MSSQL 2005数据库编码为latin1的string。 要求是将它作为utf-8导出为新的DB。 我已经写了下面的脚本来导出数据库:

var sql = require('mssql'); var csv = require("fast-csv"); var fs = require("fs"); var iconv = require('iconv-lite'); function exportCSV(tableName) { return new Promise(function (resolve, reject) { var csvStream = csv.format({headers: false, quoteColumns: true}), writableStream = fs.createWriteStream("Output/"+tableName+".csv",{encoding: "utf8"}); writableStream.on("finish", function() { console.log(tableName+" CSV file exported!"); resolve(); }); csvStream.pipe(writableStream); var request = new sql.Request(); var dateFormat=""; request.query('select * from '+tableName); request.on('row', function(row) { // do some processing on row if required csvStream.write(row); }); request.on('error', function(err) { reject(err); }); request.on('done', function(returnValue) { csvStream.end(); }); }); } 

因为,我正在做一些hiphens转换为编码的转换

 †

现在,而不是

 csvStream.pipe(writableStream); 

如果我写

 csvStream.pipe(iconv.decodeStream('latin1')) .pipe(iconv.encodeStream('utf-8')) .pipe(writableStream); 

一些时尚得到转换

 †

       

网上收集的解决方案 "将数据库以CSV格式从latin1编码的string导出到utf-8"

这是一个使用node-csv的工作程序。

 var sql = require('mssql'); var fs = require('fs'); var csv = require('csv'); var stream = fs.createWriteStream('out.csv', {encoding: 'utf8'}); var stringifier = csv.stringify({delimiter: ',', header: false, quoted: true}); var config = {user: '...', password: '...', server : '...', database: '...', stream: true }; var connection = new sql.Connection(config, function(err) { var request = new sql.Request(connection); request.pipe(stringifier).pipe(stream); request.query('SELECT * FROM table'); stream.on('error', end); stream.on('finish', end); }); var end = function(err) { if (err) { console.error('Error with file stream', err); } else { console.log('Done with file stream'); } stringifier.end(); connection.close(); };