0

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)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Welcome to Stack Overflow. Please edit the question to include any necessary source code, data, and error messages as text. Consider [these reasons and guidelines](https://meta.stackoverflow.com/a/285557). – bad_coder Apr 29 '21 at 13:05
  • Does this answer your question? [String formatting in Python](https://stackoverflow.com/questions/517355/string-formatting-in-python) – Tomerikoo Apr 29 '21 at 13:09

1 Answers1

0

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)
DSteman
  • 1,388
  • 2
  • 12
  • 25