Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js/MySQL: Some string encodings (Emoji) are throwing errors when I try to insert into database

I've got a node.js script running that pulls data out of a public "database" (it's a 'blockchain') and then performs some operations on it and then inserts it into a MySQL database. I've got the MySQL database using UTF8_general_ci encoding. The vast majority of data parses fine, but every so often it hits something it can't insert. I get this error:

code: 'ER_TRUNCATED_WRONG_VALUE_FOR_FIELD',
  errno: 1366,
  sqlMessage: 'Incorrect string value: \'\\xF0\\x9F\\x8D\\x95 N...\' for column \'body\' at row 1',
  sqlState: 'HY000',
  index: 0,

It shows the bit of string that seems to be causing the error, and it always has this distinct question mark character:

This looks like Paradise for me! ����\

I'm guessing this is an encoding issue? Is there a way I can convert these before it throws an error? I'm not sure what encoding this blockchain uses, and I'm not even sure how I'd find out.

edit: here's what another example (of the error) shows on the web interface to this blockchain:

And your very welcome !👍👍👍💕💕💕

another edit: I should point out that I am using mysql.format(sql, inserts) to handle inadvertent sql problems with the data -https://github.com/mysqljs/mysql#preparing-queries

like image 940
erv Avatar asked Dec 21 '25 14:12

erv


1 Answers

The likely answer is that the MySQL connection did not specify utf8mb4. (Specifically, MySQL's utf8 will not suffice for Emoji.) Can you provide the connection code? Here is a fallback: Execute this after connecting to MySQL:

SET NAMES utf8mb4

Another possible answer is that the web server is not treating the page as UTF-8. One way to solve this is with this in the <head>:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

And the column needs to be CHARACTER SET utf8mb4, not ut8.

node.js connection pooling

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

See stackoverflow

like image 199
Rick James Avatar answered Dec 24 '25 04:12

Rick James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!