我有一個sqlalchemy核心批量更新查詢,我需要以編程方式傳遞要更新的列的名稱。如何在SQLAlchemy Core中將列的名稱作爲參數傳遞?
函數看起來如下面每個變量註釋:
def update_columns(table_name, pids, column_to_update):
'''
1. table_name: a string denoting the name of the table to be updated
2. pid: a list of primary ids
3. column_to_update: a string representing the name of the column that will be flagged. Sometimes the name can be is_processed or is_active and several more other columns. I thus need to pass the name as a parameter.
'''
for pid in pids:
COL_DICT_UPDATE = {}
COL_DICT_UPDATE['b_id'] = pid
COL_DICT_UPDATE['b_column_to_update'] = True
COL_LIST_UPDATE.append(COL_DICT_UPDATE)
tbl = Table(table_name, meta, autoload=True, autoload_with=Engine)
trans = CONN.begin()
stmt = tbl.update().where(tbl.c.id == bindparam('b_id')).values(tbl.c.column_to_update==bindparam('b_column_to_update'))
trans.commit()
的table
參數被接受並正常工作。
作爲參數傳遞時,column_to_update
不起作用。它失敗並出現錯誤raise AttributeError(key) AttributeError: column_to_mark
。如果我硬編碼列名稱,查詢運行。
如何通過column_to_update
的名稱使SQLAlchemy識別它?
編輯:最終腳本
由於@Paulo,最終的腳本是這樣的:
def update_columns(table_name, pids, column_to_update):
for pid in pids:
COL_DICT_UPDATE = {}
COL_DICT_UPDATE['b_id'] = pid
COL_DICT_UPDATE['b_column_to_update'] = True
COL_LIST_UPDATE.append(COL_DICT_UPDATE)
tbl = Table(table_name, meta, autoload=True, autoload_with=Engine)
trans = CONN.begin()
stmt = tbl.update().where(
tbl.c.id == bindparam('b_id')
).values(**{column_to_update: bindparam('b_column_to_update')})
CONN.execute(stmt, COL_LIST_UPDATE)
trans.commit()
因爲批量更新需要此綁定參數,我該如何將'bindparam(b_column_to_mark)'傳遞給查詢? – lukik
第二個選項更多地是我想要的,因爲我已經看到'getattr'獲取列名稱。然而,當我運行它時,出現錯誤'AttributeError:'BinaryExpression'對象和'Comparator'對象都沒有屬性'items'' – lukik
'values'方法使用像''這樣的命名參數。值(column_to_update = value)'其中'column_to_update'是實際的列名稱,而不是包含列名稱的變量。如果你需要它是動態的,使用'** kwargs'符號:'.values(** {'column_to_update':value})' –