gpt4 book ai didi

python - 为什么类型错误为: __init__() takes 1 positional argument but 2 were given when running sql commands

转载 作者:行者123 更新时间:2023-11-29 17:38:48 25 4
gpt4 key购买 nike

import mysql.connector

conn = mysql.connector.connect(host='localhost', user='root', password='root', database='nlidb')
cur = conn.cursor()
stmt = "create table statistics(sno int(5), stat_ticker varchar(10) primary key, marketcap varchar(10), enterprise_value varchar(10), return_on_assets varchar(10), total_cash varchar(10), operating_cash_flow varchar(10), levered_free_cash_flow varchar(10), total_debt varchar(10), current_ratio varchar(10), gross_profit varchar(10), profit_margin varchar(10))"
cur.execute(stmt)

我正在尝试在数据库中创建一个表,但它给了我这个错误。

Traceback (most recent call last):
File "G:/python/NLIDB/nlidb/database.py", line 3, in <module>
conn = mysql.connector.connect('localhost', user='root', password='root', database='nlidb')
File "G:\python\NLIDB\venv\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "G:\python\NLIDB\venv\lib\site-packages\mysql\connector\connection.py", line 57, in __init__
super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given

谁能帮我解决这个问题吗?提前致谢。

最佳答案

从我在错误代码中注意到的

    File "G:/python/NLIDB/nlidb/database.py", line 3, in <module>
conn = mysql.connector.connect('localhost', user='root', password='root',
database='nlidb')"

你缺少传递 conn = mysql.connector.connect(host='localhost',...)

如果您只是将用户、密码等的值分配给变量,它也无法与 mysql 模块一起使用:

    user = 'root'
password = 'root'
host = 'localhost'
database = 'db'

然后尝试运行这样的行:

    conn = mysql.connector.connect(host, user, password, database)

但是,您可以先为变量赋值,然后编写以下行:

    conn = mysql.connector.connect(host=host, user=user, password=password, database=database)

希望对某人有帮助:)

关于python - 为什么类型错误为: __init__() takes 1 positional argument but 2 were given when running sql commands,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50111591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com