简体   繁体   中英

Problems with inserting emojis into database using mysql.connector in python

As described in the title, I have a problem with inserting emojis into a MariaDB database using the mysql.connector module in python.

When I try to insert the data containing emojis into the database with python, all emojis are simply replaced by question marks. I have also made sure that the emojis work in python by outputting them to the console before inserting them into the database.

Inserting the emojis via phpMyAdmin is no longer a problem after I changed the collation in the database to utf8mb4_general_ci . So I could make sure that there is no problem with the database.

If it helps in any way the data I'm inserting comes from the steam API and consists of nicknames.

I would now like to know if anyone knows how I can fix this problem.

I hope I have not forgotten any important information.

You are correct that emoji are representable in Unicode. Did you remember to set the connection character set and collation in your Python program?

Something like this might help right as you open the connection .

cnx = mysql.connector.connect(user='JonaWe', database='test',
                              use_unicode=True,
                              charset='utf8mb4,
                              collation='utf8mb4_general_ci',
                              etcetera)
cnx.set_charset_collation('utf8mb4', 'utf8mb4_general_ci')

The connection between Python and MySQL might be garbling the utf8mb4 contents of your emoji.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM