【问题标题】:How to check if a column exists in a table with python pyodbc?如何使用 python pyodbc 检查表中是否存在列?
【发布时间】:2020-09-11 07:34:30
【问题描述】:

我正在动态地将列添加到 sql 表中,我面临的问题是检查列是否存在。

我使用的代码:

 table_name = "mytable.name"
    columns = [
    {
    "field": "name",
    "data_type": "varchar",
    "length": "50"
    },
    {
    "field": "register",
     "data_type": "int"
    }
    ]
for col in columns:
if not cursor.tables(table=table_name, tableType='TABLE').fetchone():
   do some process
else:
   do some process

这里的问题是我还需要检查该列是否存在。我怎样才能实现它?

【问题讨论】:

    标签: python mysql sql pyodbc


    【解决方案1】:

    解决方法比较简单,如下:

    column_check = ("SELECT * FROM information_schema.columns WHERE table_name  = {} and column_name = {}").format(your_table, your_column)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-31
      • 2013-08-07
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 2011-03-19
      相关资源
      最近更新 更多