Answer the question
In order to leave comments, you need to log in
How to solve encoding error (Python and MySQLdb)?
For help! I started working with Python very recently. Wrote a scraper that pulls metadata from Google Play. The data is taken and displayed normally (i.e. Cyrillic does not fray), but when I try to write this data to the database (I use the MySQLdb module), as soon as an entry with Cyrillic is found in the field, it throws the following:
_mysql_exceptions.OperationalError: (1366, “Incorrect string value: '\\xF0\\x9F\\x8D\\x80' for column 'title' at row 1”)
conn = MySQLdb.connect('localhost', 'VProgramMist', '1998Vm0000', 'mobasta_history', charset='utf8', init_command='SET NAMES UTF8')
sql_str = """INSERT
INTO history (app_id, title, score, price, free, iap,
iap_range, size, installs, content_rating,
date_point, category, collection)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
cursor.execute(sql_str, (
row['app_id'], row['title'], row['score'],
row['price'], row['free'], row['iap'],
row['iap_range'], row["size"], row['installs'],
row['content_rating'][0], datetime.date.today(),
row['category'][0], row['collection']))
Answer the question
In order to leave comments, you need to log in
https://mysqlclient.readthedocs.io/user_guide.html...
use_unicode
If True, CHAR and VARCHAR and TEXT columns are returned as Unicode strings, using the configured character set. It is best to set the default encoding in the server configuration, or client configuration (read with read_default_file). If you change the character set after connecting (MySQL-4.1 and later), you'll need to put the correct character set name in connection.charset.
If False, text-like columns are returned as normal strings, but you can always write Unicode strings.
This must be a keyword parameter.
charset
If present, the connection character set will be changed to this character set, if they are not equal. Support for changing the character set requires MySQL-4.1 and later server; if the server is too old, UnsupportedError will be raised. This option implies use_unicode=True, but you can override this with use_unicode=False, though you probably shouldn't.
If not present, the default character set is used.
This must be a keyword parameter.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question