mysql - 使用nodejs将mysql查询从latin1转换为utf8

标签 mysql node.js encoding latin1 node-mysql

我在 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。这可以通过两种方式完成:

  1. 使用查询set names utf8 设置连接字符集。
  2. 在创建连接时设置连接字符集。不知道这在 node.js 中是否可行。如果不使用上述选项。

关于mysql - 使用nodejs将mysql查询从latin1转换为utf8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20884726/

相关文章:

node.js - nodejs+Q promise : no Reference Exception in the fulfillment handle

java - Base64 编码位图返回冗长的字符串

mysql - 如何将SQL中日期时间字段的默认值设置为当前时间戳加上固定的时间跨度?

php - 检索 MySQL 行键

javascript - Node.js 条件要求

node.js - 调试 Node 项目(botpress)

python - 使用 StringIO 对象保存 uu.encode/uu.decode 数据

php - 从 MySQL 到 XML 再到 HTML 的编码问题

android - 设计一个结构化数据库来聊天,一张表或多张表

php - 通过 subject_id 获取讲师姓名