EncodeError: 'latin-1' codec can't encode characters in position 69-70: ordinal not in range(
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 69-70: ordinal not in range(256)解决办法
使用python3.X向数据库插入中文数据的时候,提示:
大概步骤是:
anjuke_area = (513, '平阴', 'http://jinan.anjuke.com', '2017-03-29 13:17:03')
print('开始链接数据库')
conn = pymysql.connect( host='XX.XX.XX.XX', port=3306, user='root', passwd='root123', db='数据库名称')
print('链接数据库成功')
cur = conn.cursor()
cur.execute("INSERT INTO anjuke_area(id, name, link, time) VALUES (%s,%s,%s,%s)",anjuke_area)
然后提示:
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 69-70: ordinal not in range(256)
实际解决问题的办法是。。。。。:
conn = pymysql.connect( host='XX.XX.XX.XX', port=3306, user='root', passwd='root123', db='数据库名称', charset="utf8")
最后增加一个 : , charset="utf8" 就好了
今日事今日毕