首页 > 解决方案 > 在现有数据库中创建表。但用户使用输入变量选择数据库名称

问题描述

我正在尝试连接到数据库并根据用户的输入创建表,但出现此错误:TypeError: __init__() takes 1 positional argument but 6 were given

这是我的代码:

import mysql.connector

def add_tb():
    host='localhost'
    port='3306'
    user='root'
    password=''
    database =input('type the database name you want to add the table inside of it: ')
    
    mydb = mysql.connector.connect(host, port, user, password, database)
    mycursor = mydb.cursor()

    tablename=input('what the name of table u want to create ?:')
    mycursor.execute(f'create table {tablename} (name varchar (200), kelas int (4))') 

add_tb()

但我得到了这个错误:

Traceback (most recent call last):
  File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton\ALDOBOT\buattabel.py", line 18, in <module>
    add_tb()
  File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton\ALDOBOT\buattabel.py", line 13, in add_tb
    mydb = mysql.connector.connect(host, port, user, password, database)
  File "C:\Users\NBUSER\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\__init__.py", line 273, in connect
    return MySQLConnection(*args, **kwargs)
PS C:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton> & C:/Users/NBUSER/AppData/Local/Programs/Python/Python39/python.exe c:/Users/NBUSER/Documents/ARTOFWAR/PITON/02-piton/ALDOBOT/buattabel.py
type database you wanttest123
Traceback (most recent call last):
  File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton\ALDOBOT\buattabel.py", line 18, in <module>
    add_tb()
  File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton\ALDOBOT\buattabel.py", line 13, in add_tb
    mydb = mysql.connector.connect(host, port, user, password, database)
  File "C:\Users\NBUSER\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\__init__.py", line 273, in connect
    return MySQLConnection(*args, **kwargs)
  File "C:\Users\NBUSER\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 72, in __init__
TypeError: __init__() takes 1 positional argument but 6 were given
PS C:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton> & C:/Users/NBUSER/AppData/Local/Programs/Python/Python39/python.exe c:/Users/NBUSER/Documents/ARTOFWAR/PITON/02-piton/ALDOBOT/buattabel.py
type the database name you want to add the table inside of it: test123
Traceback (most recent call last):
  File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton\ALDOBOT\buattabel.py", line 16, in <module>
    add_tb()
  File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton\ALDOBOT\buattabel.py", line 10, in add_tb
    mydb = mysql.connector.connect(host, port, user, password, database)
  File "C:\Users\NBUSER\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\__init__.py", line 273, in connect
    return MySQLConnection(*args, **kwargs)
  File "C:\Users\NBUSER\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 72, in __init__
    super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: __init__() takes 1 positional argument but 6 were given

标签: mysqlpython-3.xxampp

解决方案


你得到的错误是因为为了能够接受任意数量的关键字参数而mysql.connector.connect()使用(“kwargs”表示“关键字参数”),这就是它不按特定顺序接受参数的原因。**kwargs

它需要提供key=value对格式。

这是您需要使用它的方式:

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

在此处查看更多参数名称


推荐阅读