Unicode and Encoding in Python and MySql
When dealing with Unicode data, Python and MySQL require careful consideration of encoding to avoid errors such as the one you encountered. The error message suggests that the characters in your JSON data are not being properly encoded for storage in your MySQL table.
To address this issue, you have two options:
Modifying the Database Table:
Handling Encoding in Python:
Here is an updated Python code segment that incorporates the charset argument:
cur = conn.cursor() cur.execute("SET NAMES utf8") cur.execute("INSERT INTO yahoo_questions (question_id, question_subj, question_content, question_userId, question_timestamp," "category_id, category_name, choosen_answer, choosen_userId, choosen_usernick, choosen_ans_timestamp)" "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (row[2], row[5].encode('utf-8'), row[6].encode('utf-8'), quserId, questionTime, categoryId, categoryName, qChosenAnswer.encode('utf-8'), choosenUserId, choosenNickName, choosenTimeStamp))
Ensure that your database variables are set correctly as well. The character_set_database variable should be set to utf8 to match the table and connection settings.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3