Lzjss
V2EX  ›  Python

Python 连接 mysql 失败 TypeError: __init__() takes 1 positional argument but 4 were given

By Lzjss at 2021 年 5 月 16 日 · 2883 次点击
首先我非常确定各个参数是没错的
然后我试过给各个参数加上名称 例如 host=self.host 也是不行

CODE:

def __init__(self, host=None, username=None, password=None, database=None, config_file=None, dbms="mysql"):
"""
Class constractor
:param host:
:param username:
:param password:
:param database:
"""
self._host = host
self._username = username
self._password = password
self._database = database
self._run_transaction = None
if config_file:
self.get_mysql_credentials(config_file, dbms)

def get_mysql_credentials(self, configfile, dbms):
"""
Retrieve credentials from configuration file
:param configfile: path to
:param dbms: default is mysql
:return: VOID
"""
try:
if configfile:
config = configparser.ConfigParser()
config.read(configfile)
self._host = config[dbms]['host']
self._username = config[dbms]['username']
self._password = config[dbms]['password']
self._database = config[dbms]['database']
self._run_transaction = config[dbms]['transactions']
except:
print("Error: couldn't read config file")

def connect(self, database=None):
"""
Connect to a existing database
:param database: name
:return: the connection handler
"""
try:
if database: # if is not Null
self._database = database
connection = pymysql.connect(self._host, self._username, self._password, self._database)
return connection
except pymysql.InternalError as error:
print(error.args[1])

config_file:::
[mysql]
host = localhost
username = root
password = passwordForCSC675
database = PharmacyManagementSystemDB
transactions = 1
11 条回复  •  2021-05-19 09:32:16 +08:00
hello2060
   1
hello2060  
   2021 年 5 月 16 日 via iPhone
手机上没法好好看,是 pymysql.connect(self._host, self._username, self._password, self._database) 出错了?你给了 4 个参数实际只要一个?
zagfai
   2
zagfai  
   2021 年 5 月 16 日   ❤️ 2
乱 78 糟谁给你看啊
wellsc
   3
wellsc  
   2021 年 5 月 16 日   ❤️ 1
报错信息不是给你了,你需要做的就是把英文读懂
lscho
   4
lscho  
   2021 年 5 月 16 日
我寻思着报错提示也太明显了吧,这还纠结什么链接 mysql 失败,都提示了 __init__() 参数问题了
crystom
   5
crystom  
   2021 年 5 月 16 日
init 那个类初始化错误,你把 name=value 这样的参数和基于顺序的参数搞错了
johnsona
   6
johnsona  
   2021 年 5 月 16 日 via iPhone
好家伙 我跟你讲 学前端 学 java 就是不学 python
Lzjss
   7
Lzjss  
OP
   2021 年 5 月 19 日
@hello2060 意思是这个意思,是需要 declare 那些 parameters 才可以
Lzjss
   8
Lzjss  
OP
   2021 年 5 月 19 日
@crystom 你是对的, 我 declare 那些 parameters 后可以了 就像 connection = pymysql.connect(host=self._host, user=self._username, password= self._password, database=self._database)
Lzjss
   9
Lzjss  
OP
   2021 年 5 月 19 日
@johnsona 我这学期才用到的 python,之前用的 java
Lzjss
   10
Lzjss  
OP
   2021 年 5 月 19 日
@wellsc 这个你放心,肯定能读懂
Lzjss
   11
Lzjss  
OP
   2021 年 5 月 19 日
@lscho 老师视频演示的没问题,原来是因为 pymysql 的版本问题导致的
推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
© 2026 V2EX · 43ms · 3.9.8.5