python 向MySQL裏插入中文數據

用python向MySQL中插入中文數據出錯,原代碼片斷:python

1 db = MySQLdb.connect("localhost","root","123","TESTDB")
2 cursor = db.cursor()
3 sql = '''INSERT INTO aaaa(bb,cc) values(%s , %s)''' 
4 
5 data = ("山東","威海")
6 cursor.execute(sql,data)
7 
8 conn.commit()
9 conn.close()

運行提示:sql

UnicodeEncodeError: 'latin-1' codec can't encode character u'\u674e' in position 0: ordinal not in range(256)

解決方案:code

db = MySQLdb.connect("localhost","root","123","TESTDB",use_unicode=1, charset='utf8')
相關文章
相關標籤/搜索