使用pymysql连接数据库报错__init__() takes 1 positional argument but 5 positional arguments (and 1 keyword-only argument) were given
核心问题就是py3.8中使用使用数据库连接的时候前面要有指定的连接名称
很多教材上面连接就是直接的写
db = pymysql.connect("localhost", "root", "196811", "db_student",charset="utf8")
return db # 返回连接对象
这个方法是直接会导致报错我们使用下面固定参数字段的方法
try:
db = pymysql.connect(host="localhost", user="root", password="196811", database="db_student",charset="utf8")
print("数据库连接成功")
except pymysql.Error as e:
print("数据库连接失败:"+str(e))
到此就连接成功了!
在Python 3.8中使用pymysql连接数据库时,遇到`__init__()`报错,原因是参数传递方式不正确。传统的连接方式会导致错误。正确做法是使用关键字参数来指定连接参数,例如`host`、`user`、`password`等。以下是修正后的代码示例,确保连接成功并打印状态信息。
3002





