我在 latin1 中有一个数据库,我想将其查询转换为 utf-8。
例子:
response.write 返回此 JSON:
{"questions":[{"question":"Nação","answer":"PaÃs"}]}
但我需要将其转换为 utf8:
{"questions":[{"question":"Nação","answer":"País"}]}
conexao_bd.getConnection(function(err,con){
if(err) return console.log("Erro na conexao. "+err);
con.query("SELECT question, answer FROM "+tipo+" WHERE id IN "+c,function(err, rows){
if(err) console.log("Erro na query questions: "+err);
else{
for(var k =0;k<rows.length;k++)
perguntas.questions[k]={'question': rows[k].question,'answer':rows[k].answer};
console.log(JSON.stringify(perguntas));
con.release();
response.writeHeader(200, {"Content-Type": "text/plain"});
response.write(JSON.stringify(perguntas));
response.end();
}
});
});
最佳答案
@bodokaiser 发布的重新编码字符串也是一种可能性,但这不是一个干净的系统解决方案。干净的解决方案是告诉数据库给你 utf-8。这可以通过两种方式完成:
- 使用查询
set names utf8
设置连接字符集。 - 在创建连接时设置连接字符集。不知道这在 node.js 中是否可行。如果不使用上述选项。
关于mysql - 使用nodejs将mysql查询从latin1转换为utf8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20884726/