后浪云Python教程:python中进行sql查询出现乱码怎么解决
通过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): 连接编码
版权声明:
作者:后浪云
链接:https://www.idc.net/help/182605/
文章版权归作者所有,未经允许请勿转载。
THE END