Python3链接数据库报错:Connection.__init__() takes 1 positional argument but 5 positional arguments (and 1 keyword-only argument) were given


一、现象

Python3链接数据库报错:Connection.__init__() takes 1 positional argument but 5 positional arguments (and 1 keyword-only argument) were given

 

二、解决

 

 把以下红色位置:

import pymysql
# 打开数据库连接
try:
db = pymysql.connect("localhost", "您的用户名", "您的密码", "数据库名称", charset='utf8' )
print('数据库连接成功')
# 使用cursor()方法获取操作游标
cursor = db.cursor()

# 使用execute方法执行SQL语句
cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取一条数据
data = cursor.fetchone()

print(data)

# 关闭数据库连接
db.close()
except pymysql.Error as e:
print('数据库连接失败:' + str(e))

更新后即:

import pymysql
# 打开数据库连接
try:
db = pymysql.connect(host="localhost", user="您的用户名", password="您的密码", database="数据库名称", charset='utf8' )
print('数据库连接成功')
# 使用cursor()方法获取操作游标
cursor = db.cursor()

# 使用execute方法执行SQL语句
cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取一条数据
data = cursor.fetchone()

print(data)

# 关闭数据库连接
db.close()
except pymysql.Error as e:
print('数据库连接失败:' + str(e))

三、总结

Mysqldb 与 pymysql 在写法上有一样的区别




免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM