javascript - Exporting DB as CSV from latin1 encoded strings in to utf-8 -
we have mssql 2005 db strings encoded latin1. requirement export utf-8 new db. have written following script export 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 * '+tablename); request.on('row', function(row) { // processing on row if required csvstream.write(row); }); request.on('error', function(err) { reject(err); }); request.on('done', function(returnvalue) { csvstream.end(); }); }); }
since, doing no conversion of encoding hiphens converted
â€
now, instead of
csvstream.pipe(writablestream);
if write
csvstream.pipe(iconv.decodestream('latin1')) .pipe(iconv.encodestream('utf-8')) .pipe(writablestream);
some hiphen converted to
â€Â
this working program using 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 * table'); stream.on('error', end); stream.on('finish', end); }); var end = function(err) { if (err) { console.error('error file stream', err); } else { console.log('done file stream'); } stringifier.end(); connection.close(); };
Comments
Post a Comment