我嘗試通過用戶輸入定義資料庫名稱,然后用戶可以在其中創建表。請幫我。這是我的代碼
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()
uj5u.com熱心網友回復:
你得到的錯誤是因為mysql.connector.connect()使用**kwargs為了能夠接受任意數量的關鍵字引數(“kwargs”的意思是“關鍵字引數”),這就是為什么它不按特定順序接受引數。
它需要以key=value對格式提供。
這是您需要使用它的方式:
mydb = mysql.connector.connect(host=host, port=port, user=user, password=password, database=database)
在此處查看更多引數名稱
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/322115.html
