使用pymysql的Python无法将字段添加到mysql数据库中的表中

问题描述 投票:0回答:1
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)

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

python mysql pymysql
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.