python - 使用 Shell 脚本在 mySQL Workbench 模型中添加列

标签 python mysql shell

我需要将以下两列添加到模型(mwb 文件)中的所有表中

  • 已创建
  • 已更新

因为有很多表需要修改,所以我没有手动修改,而是找到了following script

# get a reference to the schema in the model. This will get the 1st schema in it.
schema = grt.root.wb.doc.physicalModels[0].catalog.schemata[0]
# iterate through all tables
for table in schema.tables:
    # create a new column object and set its name
    column = grt.classes.db_mysql_Column()
    column.name = "created"
    # add it to the table
    table.addColumn(column)
    # set the datatype of the column
    column.setParseType("TIMESTAMP", None)
    column.defaultValue = "CURRENT_TIMESTAMP"

    # same thing for the update_time column
    column = grt.classes.db_mysql_Column()
    column.name = "updated"
    table.addColumn(column)
    column.setParseType("TIMESTAMP", None)

脚本工作正常(我将列添加到所有表中),但这些列添加在每个表的末尾。

我想将它们添加到第二个和第三个位置,就在第一个位置的主键之后。

我查看了db_mysql_Column定义,但我看不到与该职位相关的任何数据。

此外,addColumn方法不允许重载某种位置

有办法吗?

最佳答案

我不使用 python,但使用以下信息: MySQL syntax error using python to add column to a table 你应该能够做到这一点:

ALTER TABLEmytableADDnewcolINT NOT NULL AFTERidcol;

使用cursor.execute(query)

就我个人而言,我会使用 sh 循环遍历表名列表并将 alter stmts 写入一个文件,然后通过 mysql 客户端执行该文件。我就是有点懒。

关于python - 使用 Shell 脚本在 mySQL Workbench 模型中添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44160033/

相关文章:

python - PySide6:加载共享库时出错:libQt6DesignerComponents.so.6:无法打开共享对象文件

sql - 连接查询出现问题

mysql - GROUP BY 需要很长时间

linux - 我在 shell 脚本中的 shell 脚本没有获取 int 变量

arrays - 将命名数组传递给另一个 bash 脚本

python - Python Azure Web/辅助角色中的日志控制台输出

python - 凌乱的 Excel 日期支持 Python Pandas 日期

linux - 在特定目录中查找文件

python - Pandas 中的自加入产生不需要的重复项

php - 在 mysql 查询的前两行之后插入不同的行