mysql - 在现有数据库中创建表。但用户使用输入变量选择数据库名称
问题描述
我正在尝试连接到数据库并根据用户的输入创建表,但出现此错误: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
解决方案
你得到的错误是因为为了能够接受任意数量的关键字参数而mysql.connector.connect()
使用(“kwargs”表示“关键字参数”),这就是它不按特定顺序接受参数的原因。**kwargs
它需要提供key=value
对格式。
这是您需要使用它的方式:
mydb = mysql.connector.connect(host=host, port=port, user=user, password=password, database=database)
推荐阅读
- node.js - 我可以检查我的机器人中的哪些文件给了我错误消息吗?
- python - 执行 python 弹性脚本时出现 503 TransportError
- java - 将新用户输入存储为 Discord 机器人(Java、IntelliJ)的参考
- c# - SQL 字符串大小减少
- jenkins-pipeline - 用于创建带有描述的文件夹的詹金斯管道示例
- java - 如何使用 Spring Boot 返回成功消息?
- algorithm - 寻找作为给定序列的子序列出现的最长弱孪生
- mysql - 如何在 MySQL 的 WHERE 和 ORDER BY 子句中查询索引?
- r - 为什么这个闪亮的应用程序中显示的闪亮反馈错误消息有时不会改变?
- facebook - 如何通过视频获取人口统计数据?