我们有MSSQL 2005 DB,字符串编码为latin1。要求是将其导出为新数据库的utf-8。我编写了以下脚本来导出数据库:
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);
一些hiphen转换为
â€Â
答案 0 :(得分:0)
这是一个使用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();
};