在表中添加列[关闭]

Posted

技术标签:

【中文标题】在表中添加列[关闭]【英文标题】:Add Column in a table [closed] 【发布时间】:2021-04-29 11:45:54 【问题描述】:

我正在尝试在表 salesperson 中添加一列。它显示错误。请帮忙。我用的是最新版的pycharm

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)

【问题讨论】:

欢迎来到 Stack Overflow。请编辑问题以包含任何必要的源代码、数据和错误消息作为文本。考虑these reasons and guidelines。 这能回答你的问题吗? String formatting in Python 【参考方案1】:

您似乎在命令变量中使用了名称和数据变量,但您以错误的顺序声明它们。此外,您的命令变量应该是一个 f 字符串。我建议你尝试这样的事情:

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)

【讨论】:

以上是关于在表中添加列[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何在多对多表中添加列(Django)

在表中为每个数据库查找具有空值的列[关闭]

如何在一个DataGridView中的一列添加DateTimePicker控件 C#

如何在表中添加具有默认值的列

使用 C# 出现错误“无法在表中插入标识列的显式值”

mysql在表的某一位置增加一列删除一列修改列名