When I use python to apply the pymysql module to add a field to a table in the mysql database, it does not return failure. However, when I query the database, I find that the new field is not displayed.
In addition, when I run the new field script again, it returns pymysql.err.InternalError: (1060, "Duplicate column name 'xxx xxx'")


以下是一个新的字段脚本

import pymysql
textdb = {
    "host": "xx.xxx.xxx.xxx",
    "database": "textdbs",
    "user": "root",
    "password": "xxxxxxxxx",
    "port": 3306,
    "charset": 'utf8'
}
db = pymysql.connect(**textdb)
course = db.cursor()
sql = "alter table `{}` add `{}` varchar(255) default NULL".format("table", "testund")
course.execute(sql)


这是应用于远程数据库的脚本。刚开始,我觉得脚本有问题。在本地数据库中进行实验后,我发现可以正常添加字段。

最佳答案

course.commit()之后添加course.executr(sql)

关于python - 使用pymysql的Python无法向mysql数据库中的表添加字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59191464/

10-15 19:03