TypeError: MySQLConnectionAbstract.__init__() takes 1 positional argument but 2 were given
TypeError: MySQLConnectionAbstract.__init__() takes 1 positional argument but 2 were given
我收到以下错误:
TypeError: MySQLConnectionAbstract.__init__() takes 1 positional argument but
2 were given
这是我的 db.py
:
import mysql.connector
localhost = "localhost"
username = "Admin"
password = "test123"
db = "testing"
Connect = "host = '{}', user = '{}', password = '{}', db = '{}'".format(localhost, username, password, db)
cnn = mysql.connector.connect(Connect) # line 16 error
# cnn = mysql.connector.connect(host ="localhost", user = "Admin", password = "test123", db = "testing")
如果我像下面这样编写正确的连接,那么它就起作用了。
cnn = mysql.connector.connect(host ="localhost", user = "Admin", password = "test123", db = "testing"
但是,如果我这样写代码就不行了:
cnn = mysql.connector.connect(Connect)
这是打印到终端的完整错误:
Traceback (most recent call last):
File "c:\Users\herok\Documents\MyApp 3\db.py", line 16, in <module>
cnn = mysql.connector.connect(Connect)
File "C:\Users\herok\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\__init__.py", line 273, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\herok\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection.py", line 72, in __init__
super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: MySQLConnectionAbstract.__init__() takes 1 positional argument but 2 were given
connect() 方法无法解析连接字符串,这就是引发错误的原因。
您可以在此处找到完整的参数列表:
https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
使用此方法时
cnn = mysql.connector.connect(Connect)
您正在将字符串传递给连接函数,但该函数不接受字符串,它应该像在第二种方法中那样接收关键字参数
cnn = mysql.connector.connect(host ="localhost", user = "Admin", password = "test123", db = "testing"
因此,如果您需要将连接字符串参数保存在变量中,以便将其传递给连接函数
您可以将数据保存为字典添加如下调用函数时展开字典
connection_string_params = {
"host":"localhost",
"user":"Admin",
"password":"test123",
"db":"testing"
}
cnn = mysql.connector.connect(**connection_string_params)
我收到以下错误:
TypeError: MySQLConnectionAbstract.__init__() takes 1 positional argument but
2 were given
这是我的 db.py
:
import mysql.connector
localhost = "localhost"
username = "Admin"
password = "test123"
db = "testing"
Connect = "host = '{}', user = '{}', password = '{}', db = '{}'".format(localhost, username, password, db)
cnn = mysql.connector.connect(Connect) # line 16 error
# cnn = mysql.connector.connect(host ="localhost", user = "Admin", password = "test123", db = "testing")
如果我像下面这样编写正确的连接,那么它就起作用了。
cnn = mysql.connector.connect(host ="localhost", user = "Admin", password = "test123", db = "testing"
但是,如果我这样写代码就不行了:
cnn = mysql.connector.connect(Connect)
这是打印到终端的完整错误:
Traceback (most recent call last):
File "c:\Users\herok\Documents\MyApp 3\db.py", line 16, in <module>
cnn = mysql.connector.connect(Connect)
File "C:\Users\herok\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\__init__.py", line 273, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\herok\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection.py", line 72, in __init__
super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: MySQLConnectionAbstract.__init__() takes 1 positional argument but 2 were given
connect() 方法无法解析连接字符串,这就是引发错误的原因。
您可以在此处找到完整的参数列表: https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
使用此方法时
cnn = mysql.connector.connect(Connect)
您正在将字符串传递给连接函数,但该函数不接受字符串,它应该像在第二种方法中那样接收关键字参数
cnn = mysql.connector.connect(host ="localhost", user = "Admin", password = "test123", db = "testing"
因此,如果您需要将连接字符串参数保存在变量中,以便将其传递给连接函数
您可以将数据保存为字典添加如下调用函数时展开字典
connection_string_params = {
"host":"localhost",
"user":"Admin",
"password":"test123",
"db":"testing"
}
cnn = mysql.connector.connect(**connection_string_params)