我正在尝试创建一个脚本来显示我的数据库,有8列,所有这些都是utf8_unicode_ci。
这是剧本:
import sys
import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='root', db='rfid')
cur = conn.cursor()
cur.execute("SELECT * FROM cnf1")
for r in cur.fetchall():
print(r)
cur.close()
conn.close()
但是在运行脚本时,控制台会抛出以下错误:
(1, 'camilo', 'camilo', 'camilo', 'camilo', 'camilo', 'camilo', 'camilo')
(2, 'Juan', 'Perez', 'Sanchez', '112223330', 'Las Almendras 111', 'juan.perez@correo.com', '12AAA')
(3, 'a', 'a', 'a', 'a', 'a', 'a', 'a')
(4, 'camilo1', 'camilo1', 'camilo1', 'camilo1', 'camilo1', 'camilo1', 'camilo1')
Traceback (most recent call last):
File "/Users/camilonunezfernandez/Desktop/prueba2.py", line 11, in <module>
(5, 'camilo1', 'camilo1', 'camilo1', 'camilo1', 'camilo1', 'camilo1', 'camilo1')
(6, 'camilo1', 'camilo1', 'camilo1', 'camilo1', 'camilo1', 'camilo1', 'camilo1')
print(r)
UnicodeEncodeError: 'ascii' codec can't encode character '\xf1' in position 5: ordinal not in range(128)
[Finished in 0.4s with exit code 1]
[cmd: ['python3', '-u', '/Users/camilonunezfernandez/Desktop/prueba2.py']]
[dir: /Users/camilonunezfernandez/Desktop]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
我正在使用Python的3.4版,它运行'pymysql'库来连接到我的MySQL数据库。
他们知道这个错误可能会发生吗? 非常感谢你!
答案 0 :(得分:0)
尝试传递charset参数
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='root', db='rfid', charset='utf8')