简体   繁体   English

使用 python 中的 mysql.connector 将表情符号插入数据库的问题

[英]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.

相关问题 使用Python mysql.connector将包含表情符号的unicode API响应插入Mysql - Inserting a unicode API response containing emojis into Mysql using Python mysql.connector 将boolean插入mysql数据库时出现python mysql.connector错误 - Python mysql.connector error while inserting boolean into mysql database 使用 python 库 mysql.connector 将 csv 插入 MySQL 数据库 - Inserting csv into MySQL database with python library mysql.connector 使用 Python mysql.connector 连接到 MariaDB 的问题 - Problems connecting to MariaDB using the Python mysql.connector 使用 mysql.connector for python 连接数据库时出错 - error when using mysql.connector for python to connect database 使用Python MYSQL.Connector在MySQL表中插入时出错 - Getting error while inserting in MySQL table using Python MYSQL.Connector 使用 mysql.connector 模块从 Python 插入 MySQL - 如何使用 executemany() 插入行和子行? - Inserting into MySQL from Python using mysql.connector module - How to use executemany() to inert rows and child rows? 使用mysql.connector将值插入mysql服务器时出现问题 - Trouble inserting values into mysql server using mysql.connector 将MySql数据库值Pul到列表存储-Python3和Mysql.connector - Pul MySql Database values to liststore - Python3 and Mysql.connector Python mysql.connector错误 - Python mysql.connector error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM