0

I have a node application that queries a MySQL database and writes results to a file.

The query result is a piece of text with translations to most world languages.

My issue is that most non ASCII chars don't display properly. For example

Wir möchten

Is written to file as:

Wir möchten

This is how I connect to the database:

  const mysql = require("mysql");
  const connection = mysql.createConnection({
    host: "",
    user: "",
    password: "",
    database: "",
    charset: "utf8", // I have tried 'utf8'/'latin1'/'BIG5'
  });

This is the function that writes query result to file:

query(sql, (err, data) => {
  fs.writeFile("x.json", JSON.stringify(data), "utf8", function (err) {
    if (err) throw err;
    console.log("Saved!");
  });
});

2 Answers 2

1

I eventually found a way that works on the MySQL SELECT statement level:

SELECT CONVERT(CAST(CONVERT(column_name USING LATIN1) AS BINARY) USING UTF8) AS column_name from table_name;

I used the above statement as input to my node app and it worked for all the alphabets I need (Latin, Cyrillic, Chinese ...etc.).

Source.

Sign up to request clarification or add additional context in comments.

Comments

1

ö is "Mojibake" for ö. See Mojibake in Trouble with UTF-8 characters; what I see is not what I stored

As for the right way to fix the data you have, pick the right situation from the 7 cases here: http://mysql.rjweb.org/doc.php/charcoll#fixes_for_various_cases

Note that "double-encoding" is not the same as "Mojibake".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.