V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
Lzjss
V2EX  ›  Python

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

  •  
  •   Lzjss · 2021-05-16 13:04:17 +08:00 · 1549 次点击
    这是一个创建于 1047 天前的主题,其中的信息可能已经有所发展或是发生改变。
    首先我非常确定各个参数是没错的
    然后我试过给各个参数加上名称 例如 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-05-16 13:08:37 +08:00 via iPhone
    手机上没法好好看,是 pymysql.connect(self._host, self._username, self._password, self._database) 出错了?你给了 4 个参数实际只要一个?
    zagfai
        2
    zagfai  
       2021-05-16 13:12:02 +08:00   ❤️ 2
    乱 78 糟谁给你看啊
    wellsc
        3
    wellsc  
       2021-05-16 13:21:20 +08:00   ❤️ 1
    报错信息不是给你了,你需要做的就是把英文读懂
    lscho
        4
    lscho  
       2021-05-16 13:33:49 +08:00
    我寻思着报错提示也太明显了吧,这还纠结什么链接 mysql 失败,都提示了 __init__() 参数问题了
    crystom
        5
    crystom  
       2021-05-16 13:39:13 +08:00
    init 那个类初始化错误,你把 name=value 这样的参数和基于顺序的参数搞错了
    johnsona
        6
    johnsona  
       2021-05-16 17:30:35 +08:00 via iPhone
    好家伙 我跟你讲 学前端 学 java 就是不学 python
    Lzjss
        7
    Lzjss  
    OP
       2021-05-19 09:29:02 +08:00
    @hello2060 意思是这个意思,是需要 declare 那些 parameters 才可以
    Lzjss
        8
    Lzjss  
    OP
       2021-05-19 09:30:44 +08:00
    @crystom 你是对的, 我 declare 那些 parameters 后可以了 就像 connection = pymysql.connect(host=self._host, user=self._username, password= self._password, database=self._database)
    Lzjss
        9
    Lzjss  
    OP
       2021-05-19 09:31:13 +08:00
    @johnsona 我这学期才用到的 python,之前用的 java
    Lzjss
        10
    Lzjss  
    OP
       2021-05-19 09:31:32 +08:00
    @wellsc 这个你放心,肯定能读懂
    Lzjss
        11
    Lzjss  
    OP
       2021-05-19 09:32:16 +08:00
    @lscho 老师视频演示的没问题,原来是因为 pymysql 的版本问题导致的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   920 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:54 · PVG 04:54 · LAX 13:54 · JFK 16:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.