I have a python code which adds rows to the MySQL database which runs on the same machine. The code is as follows:
import mysql.connector
config = {
'user': 'root',
'password': 'a$$enjoyer',
'host': 'localhost',
'database': 'db',
'raise_on_warnings': True
}
cnx = mysql.connector.connect(**config)
con = cnx.cursor()
Now, I have a table which has a MEDIUMTEXT
column. The issue is that whenever I try to insert texts with emojis (yes, I need them), I get the error like: 1366 (HY000): Incorrect string value: '\xF0\x9F\xA5\xB6' for column 'epictext' at row 1
.
The code is as follows:
con.execute('INSERT INTO nice VALUES(%s,%s,%s)', [1,2,''])
I have found a few questions here which addressed this error like, for example this one, but they didn't fix the issue.
The weird thing is that simply running INSERT INTO nice VALUES(1,2,'');
directly in MySql console works fine. Moreover adding the 'collation': 'utf8mb4_general_ci'
to config
fixes the issue entirely, but when I last tried doing this, this line was not necessary, and I would really like to avoid using it if it is possible.