我正在尝试使用mysql_query语句更新数据库中的记录。
有谁能理解为什么这段代码不起作用,以及我如何设法将键值动态添加到UPDATE语句中?
...
{
"diff_records": {
"app_portfolio_manager": "New Manager"
}
}
- name: case 1b insert app_dict to db table app_info
community.mysql.mysql_query:
- UPDATE app_info
SET ( {{ diff_records.keys() | join(', ') }} ) VALUES ( {{ diff_records.values() | map('regex_replace', '^(.*)$', "'\1'") | join(', ') }} )
WHERE app_name = '{{ app_dict.app_name }}'
fatal: [localhost]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"query": [
"UPDATE app_info SET ( app_portfolio_manager ) VALUES ( 'New Manager' ) WHERE app_name = 'My new App'"
],
"single_transaction": true
}
},
"msg": "Cannot execute SQL 'UPDATE app_info SET ( app_portfolio_manager ) VALUES ( 'New Manager' ) WHERE app_name = 'My new App'' args [None]: (1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '( app_portfolio_manager ) VALUES ( 'New Manager' ) WHERE app_name = 'My new...' at line 1\")"
}
2条答案
按热度按时间fcwjkofz1#
以下代码仅适用于单个键值变量,不适用于多个
- name: case 1b insert app_dict to db table app_info community.mysql.mysql_query: - UPDATE app_info SET {{ diff_records.keys() | join(', ') }} = {{ diff_records.values() | map('regex_replace', '^(.*)$', "'\1'") | join(', ') }} WHERE app_name = '{{ app_dict.app_name }}'
afdcj2ne2#
添加一个简单的dict循环似乎已经达到了目的。