今天使用MySQLdb往MySQL插入中文數(shù)據(jù)遇到一個(gè)異常:UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-3: ordinal not in range(256)
解決辦法:
在創(chuàng)建連接的時(shí)候設(shè)置一下編碼,如:
conn = MySQLdb.connect(host="localhost", user="root", passwd="root", db="chemical", charset="utf8")