[英]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.如标题中所述,我在使用 python 中的
mysql.connector
模块将表情符号插入 MariaDB 数据库时遇到问题。
When I try to insert the data containing emojis into the database with python, all emojis are simply replaced by question marks.当我尝试使用 python 将包含表情符号的数据插入数据库时,所有表情符号都被问号替换。 I have also made sure that the emojis work in python by outputting them to the console before inserting them into the database.
我还确保表情符号在 python 中工作,方法是在将它们插入数据库之前将它们输出到控制台。
Inserting the emojis via phpMyAdmin is no longer a problem after I changed the collation in the database to utf8mb4_general_ci
.在我将数据库中的排序规则更改为
utf8mb4_general_ci
后,通过 phpMyAdmin 插入表情符号不再是问题。 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.如果它有任何帮助,我插入的数据来自蒸汽 API 并由昵称组成。
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.你是对的,表情符号可以在 Unicode 中表示。 Did you remember to set the connection character set and collation in your Python program?
您是否记得在 Python 程序中设置连接字符集和排序规则?
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. Python 和 MySQL 之间的连接可能会混淆您表情符号的 utf8mb4 内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.