![图片[1]-python中进行sql查询出现乱码怎么解决-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5eba0edf0e3be434.jpg)
通过Python查询数据库中数据时,数据汇呈现乱码,只需要在连接串中指定数据编码就可以如“utf8”、“GBK”即可:
importpymysql
db=pymysql.connect(host="localhost",user="root",password="root",database="school_schema",charset="GBK")
cursor=db.cursor()
sqlstr="SELECT*FROMschool_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): 连接编码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END

















































暂无评论内容