1. 程式人生 > >【轉】python mysql數據庫 'latin-1' codec can't encode character錯誤問題解決

【轉】python mysql數據庫 'latin-1' codec can't encode character錯誤問題解決

處理 tin sql數據庫 clas question ack RM can 意思

UnicodeEncodeError: ‘latin-1‘ codec can‘t encode character

"UnicodeEncodeError:‘latin-1‘ codec can‘t encode character ..."

This is because MySQLdb normally tries to encode everythin to latin-1. This can be fixed by executing the following commands right after you‘ve etablished the connection:

db.set_character_set(
utf8) dbc.execute(SET NAMES utf8;) dbc.execute(SET CHARACTER SET utf8;) dbc.execute(SET character_set_connection=utf8;)

"db" is the result of MySQLdb.connect, and "dbc" is the result of db.cursor().

意思就是MySQLdb正常情況下會嘗試將所有的內容轉為latin1字符集處理 所以處理方法就是,設置連接和遊標的charset為你所希望的編碼,如utf8

db是connection連接,dbc是數據庫遊標

http://stackoverflow.com/questions/3942888/unicodeencodeerror-latin-1-codec-cant-encode-character

【轉】python mysql數據庫 'latin-1' codec can't encode character錯誤問題解決