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
然后我试过给各个参数加上名称 例如 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
|
1
hello2060 2021 年 5 月 16 日 via iPhone
手机上没法好好看,是 pymysql.connect(self._host, self._username, self._password, self._database) 出错了?你给了 4 个参数实际只要一个?
|
|
2
zagfai 2021 年 5 月 16 日 乱 78 糟谁给你看啊
|
|
3
wellsc 2021 年 5 月 16 日 报错信息不是给你了,你需要做的就是把英文读懂
|
|
4
lscho 2021 年 5 月 16 日
我寻思着报错提示也太明显了吧,这还纠结什么链接 mysql 失败,都提示了 __init__() 参数问题了
|
|
5
crystom 2021 年 5 月 16 日
init 那个类初始化错误,你把 name=value 这样的参数和基于顺序的参数搞错了
|
|
6
johnsona 2021 年 5 月 16 日 via iPhone
好家伙 我跟你讲 学前端 学 java 就是不学 python
|
推荐学习书目
› 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