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