gpt4 book ai didi

javascript - 将 DB 从 latin1 编码字符串导出为 CSV 到 utf-8

转载 作者:太空宇宙 更新时间:2023-11-03 22:34:09 27 4
gpt4 key购买 nike

我们有 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();
});
});
}

因为,我没有进行编码转换,一些 hihens 被转换为

â€

现在,而不是

csvStream.pipe(writableStream);

如果我写

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

一些 hiphen 被转换为

â€Â

最佳答案

这是一个使用 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();
};

关于javascript - 将 DB 从 latin1 编码字符串导出为 CSV 到 utf-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32097471/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com