Skip to content
Advertisement

Add Column in a table [closed]

Am trying to add a column in the table salesperson. And it is showing error. Pls help. I am using the latest version of pycharm

ERROR

mycon = mysql.connector.connect(host='localhost', user='root', passwd='paswd', database='11b')
if mycon.is_connected():
    print('Connection established sucessfully.')
else:
    print('Connection not established.')
mycursor = mycon.cursor()

command = "alter table salesperson add {name} {data}"
name= input("Enter the name of column you want to :")
data= input("Enter the data type of the column:")
mycursor.execute(command)

query = "Select*from salesperson"
mycursor.execute(query)
result = mycursor.fetchall()
mycon.commit()
for i in result:
    print(i)

Advertisement

Answer

It seems like you are using the name and data variables in the command variable but you declare them in the wrong order. Also, your command variable should be a f-string. I’d suggest you try something like this:

mycon = mysql.connector.connect(host='localhost', user='root', passwd='paswd', database='11b')
if mycon.is_connected():
    print('Connection established sucessfully.')
else:
    print('Connection not established.')
mycursor = mycon.cursor()

name= input("Enter the name of column you want to :")
data= input("Enter the data type of the column:")
command = f"alter table salesperson add {name} {data}"
mycursor.execute(command)

query = "Select*from salesperson"
mycursor.execute(query)
result = mycursor.fetchall()
mycon.commit()
for i in result:
    print(i)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement