Home > database >  create table inside the existing database. but user choose the database name using input variable
create table inside the existing database. but user choose the database name using input variable

Time:10-18

i try to defining database name by user input, before the user can create a table inside of it. please help me. here is my code

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()

CodePudding user response:

the error you get is because mysql.connector.connect() uses **kwargs in order to be able to take an arbitrary number of keyword arguments ("kwargs" means "keyword arguments"), that's why it does not take arguments in a specific order.

It needs to be provided with key=value pair format.

This is how you need to use it:

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

see more argument names here

  • Related