python中进行sql查询出现乱码怎么解决
yang
2020-05-12 10:59:343704浏览 · 0收藏 · 0评论
通过Python查询数据库中数据时,数据汇呈现乱码,只需要在连接串中指定数据编码就可以如“utf8”、“GBK”即可:
import pymysql db=pymysql.connect(host="localhost",user="root",password="root",database="school_schema",charset="GBK") cursor=db.cursor() sqlstr="SELECT * FROM school_schema.course;"try: cursor.execute(sqlstr) courses=cursor.fetchall()except: print("数据读取错误")else: print(courses) db.close()
pymysql.Connect()参数说明:
host(str): MySQL服务器地址
port(int): MySQL服务器端口号
user(str): 用户名
passwd(str): 密码
db(str): 数据库名称
charset(str): 连接编码
更多Python知识请关注Python视频教程栏目。
关注公众号,随时随地在线学习