在现有数据库中创建 table。但用户使用输入变量选择数据库名称
create table inside the existing database. but user choose the database name using input variable
我正在尝试连接到数据库并根据用户的输入创建一个 table,但出现此错误: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-piton\ALDOBOT\buattabel.py", line 18, in <module>
add_tb()
File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON-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-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-piton\ALDOBOT\buattabel.py", line 18, in <module>
add_tb()
File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON-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-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-piton\ALDOBOT\buattabel.py", line 16, in <module>
add_tb()
File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON-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
你得到的错误是因为 mysql.connector.connect()
使用 **kwargs
以便能够采用任意数量的关键字参数(“kwargs”表示“关键字参数”),这就是它的原因不按特定顺序接受参数。
需要提供key=value
对格式
这是您需要使用它的方式:
mydb = mysql.connector.connect(host=host, port=port, user=user, password=password, database=database)
我正在尝试连接到数据库并根据用户的输入创建一个 table,但出现此错误: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-piton\ALDOBOT\buattabel.py", line 18, in <module>
add_tb()
File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON-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-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-piton\ALDOBOT\buattabel.py", line 18, in <module>
add_tb()
File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON-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-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-piton\ALDOBOT\buattabel.py", line 16, in <module>
add_tb()
File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON-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
你得到的错误是因为 mysql.connector.connect()
使用 **kwargs
以便能够采用任意数量的关键字参数(“kwargs”表示“关键字参数”),这就是它的原因不按特定顺序接受参数。
需要提供key=value
对格式
这是您需要使用它的方式:
mydb = mysql.connector.connect(host=host, port=port, user=user, password=password, database=database)