I attempted to connect to a mysql db using mysql connector python. My table was latin encoded. I more or less tried this from the docs:
from mysql.connector import (connection)
cnx = connection.MySQLConnection(user='scott', password='password',
host='127.0.0.1',
database='employees')
cnx.close()
But I got the error:
ERROR 1115 (42000): Unknown character set: 'utf8mb4'
Why is it finding utf8mb4
if my table is Latin encoded?
To solve the problem open the exported SQL file, search and replace the utf8mb4 with utf8 , after that search and replace the utf8mb4_unicode_520_ci with utf8_general_ci . Save the file and import it into your database. After that, change the wp-config. php charset option to utf8 , and the magic starts.
After searching around for a while, I found this regarding connections: https://dev.mysql.com/doc/refman/8.0/en/charset-connection.html which got me thinking.
I just tried adding the charset
argument to my connection like so:
cnx = connection.MySQLConnection(user='scott',
password='password',
host='127.0.0.1',
charset='utf8',
database='employees')
https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
If your characters actually use the superset utf8mb4
then this may cause problems, but if not, then this should work!
Use charset="utf8"
while creating the connection object .
myconn = mysql.connector.connect(host="localhost", user="root", passwd="user", database="user_db", charset="utf8" )
It works for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With