Python3链接数据库报错:Connection.__init__() takes 1 positional argument but 5 positional arguments (and 1 keywordonly 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 在写法上有一样的区别



原文地址:https://www.cnblogs.com/waitingbar/p/15693709.html