在node.js中从数据库获取非英语字符时遇到编码问题

zaq34kh6  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(328)

在node js中,我有非常简单的代码用于向数据库发出请求:

var mysql      = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'user',
    password: 'password',
    database: "databasename"
});

connection.connect();

connection.query('SELECT * FROM main_categories', function (error, results, fields) {
    if (error) throw error;
    console.log(results);
});

connection.end();

我正在使用以下sql脚本创建数据库和表:

CREATE DATABASE databasename
  CHARACTER SET utf8
  COLLATE utf8_unicode_ci;

CREATE TABLE main_categories (
  id   INT          NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  PRIMARY KEY (id)
);

当我尝试在mysql控制台中选择代码时,一切看起来都很好,但是当我尝试在node.js中获取结果时,在控制台日志中我看到如下内容:

[ RowDataPacket { id: 1, name: 'Візочки' },
  RowDataPacket { id: 2, name: 'ДитÑчий транÑпорт' },
  RowDataPacket { id: 3, name: 'ÐвтокріÑла' },
  RowDataPacket { id: 4, name: 'Іграшки' },
  RowDataPacket { id: 5, name: 'ДитÑча кімната' },
  RowDataPacket { id: 6, name: 'Ð’Ñе Ð´Ð»Ñ Ð³Ð¾Ð´ÑƒÐ²Ð°Ð½Ð½Ñ' },
  RowDataPacket { id: 7, name: 'ÐкÑеÑуари Ð´Ð»Ñ Ð¼Ð°Ð¼' },
  RowDataPacket { id: 8, name: 'Гігієна та доглÑд' } ]

我试着补充 charset 使用时在配置对象中 mysql.createConnection 以及任何其他解决方案,但不幸的是没有结果。
upd:查看数据库变量可能会有所帮助:

mysql> SHOW variables WHERE variable_name LIKE '%coll%' OR variable_name LIKE '%char%';
+--------------------------+-----------------------------------------------------------+
| Variable_name            | Value                                                     |
+--------------------------+-----------------------------------------------------------+
| character_set_client     | utf8                                                      |
| character_set_connection | utf8                                                      |
| character_set_database   | utf8                                                      |
| character_set_filesystem | binary                                                    |
| character_set_results    | utf8                                                      |
| character_set_server     | latin1                                                    |
| character_set_system     | utf8                                                      |
| character_sets_dir       | /usr/local/mysql-5.7.19-macos10.12-x86_64/share/charsets/ |
| collation_connection     | utf8_general_ci                                           |
| collation_database       | utf8_general_ci                                           |
| collation_server         | latin1_swedish_ci                                         |
+--------------------------+-----------------------------------------------------------+
lf5gs5x2

lf5gs5x21#

Візочки mojibake代表什么 Візочки . 如果没有utf8/utf8mb4,就会发生这种情况。尤其是node.js需要

var connection = mysql.createConnection({ ... , charset : 'utf8mb4'});
kfgdxczn

kfgdxczn2#

我加了一句 SET NAMES utf8; 在创建表之前,请执行以下操作:

SET NAMES utf8;

CREATE TABLE main_categories (
  id   INT          NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  PRIMARY KEY (id)
);

就这样!

相关问题