0

I'm trying to save emojis in my database, but I get always this error:

Error: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value: '\xF0\x9F\x98\x9D' for column `myproject`.`cards`.`description` at row 1

I have tried to create the table this way:

CREATE TABLE cards (
    id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    date VARCHAR(10) NOT NULL,
    time VARCHAR(8) NOT NULL,
    place VARCHAR(200) NOT NULL,
    instagram VARCHAR(200) NOT NULL,
    description VARCHAR(3000) NOT NULL,
    comments INT NOT NULL,
    publication_date VARCHAR(20) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

But it doesn't work.

My backend is made with node, and I insert the record into database this way:

CardCtrl.createCard = async (req, res) => {
    logger.info(`Connecting to database...`, {
        __filename
    });
    try {
        var newCard = req.body;
        newCard.comments = 0;
        newCard.publicationDate = moment().format("DD-MM-YYYY HH:mm:ss");
        console.log(newCard);

        let query = `INSERT INTO Cards(date, time, place, instagram, description, comments, publication_date) VALUES("${newCard.date}", "${newCard.time}", "${newCard.place}", "${newCard.instagram}", "${newCard.description}", "${newCard.comments}", "${newCard.publicationDate}")`;

        logger.info(`Creating card... Executing query: "${query}"`, {
            __filename
        });

        bbdd.query(query, function (error, results, fields) {
            if (error) {
                logger.error(`Card does not created. ${error}`, {
                    __filename
                });
                res.status(400).json({
                    status: "KO",
                    message: "Card does not created"
                });
                return;
            }

            logger.info(`Card created`, {
                __filename
            });
            res.status(200).json({
                status: "OK",
                message: "Card created"
            });
        });
    } catch (error) {
        logger.error(`An error has ocurred connecting to database: ${error}`, {
            __filename
        });
    }
};

It's my first time working with emojis in database and I don't know how I should do this.

How can I solve this problem?

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Does this answer your question? [How to store Emoji Character in MySQL Database](https://stackoverflow.com/questions/39463134/how-to-store-emoji-character-in-mysql-database) – Amir Saleem Apr 02 '21 at 09:35
  • Duplicate of: https://stackoverflow.com/questions/39463134/how-to-store-emoji-character-in-mysql-database – Amir Saleem Apr 02 '21 at 09:35
  • Thanks, I was watching this before, but it doesn't work for me because my database charset was utf-8. Searching in the Internet, I saw that in my pool connection, I must put ```charset: "utf8mb4"```. –  Apr 02 '21 at 09:58
  • Yes, in the answers I've shared above, one person mentioned in his answer which says `Set utf8mb4 in database connection` – Amir Saleem Apr 02 '21 at 10:08

0 Answers0