gpt4 book ai didi

python - SQL Alchemy 使用带有 bindparam 的字典列表更新表不工作

转载 作者:太空宇宙 更新时间:2023-11-04 00:31:41 25 4
gpt4 key购买 nike

我正在尝试使用 bindparam 基于字典列表执行更新,但我不确定为什么这个示例不起作用:

import sqlalchemy
from sqlalchemy import Table, exc, and_
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql.expression import bindparam
from sqlalchemy.orm import sessionmaker

con_string = 'mysql+mysqlconnector://{login}:{passwd}@{hostname}:{port}/{db}'

engine_str = con_string.format(
login=login, passwd=pwd, hostname=hostname, port=db_port, db=db_name
)

try:
engine = sqlalchemy.create_engine(engine_str, echo=False)
session = sessionmaker(bind=engine)
connection = engine.connect()
session = session(bind=connection)
Base = declarative_base()
except exc.SQLAlchemyError:
raise



def bulk_updater(data):
for i in data:
print i
t = Table('demo_table', Base.metadata, autoload_with=engine)
try:
stm = t.update().where(
t.c.trigger_service == bindparam('trigger_service')).values={package_name: 'package_name'}
connection.execute(stm, data)
finally:
session.close()

d = [{'package_name': 'something', 'trigger_service': 'somewhere'}, {'package_name': 'somesome', 'trigger_service': 'wherewhere'}]
bulk_updateer(d)

编辑

似乎问题出在此处使用 bindparam 时,因为当我强制使用该值时它可以完美运行。仍在尝试了解为什么不起作用。

t.c.trigger_service == bindparam('trigger_service')

最佳答案

忽略所有周围的功能 - 和错误 - 并专注于更新,我们可以将您的示例简化为

In [2]: t = Table('demo_table', metadata,
...: Column('trigger_service', String()),
...: Column('package_name', String()))

In [5]: stmt = t.update().\
...: where(t.c.trigger_service == bindparam('trigger_service'))

省略了明确的 values()并依赖于 bindparam() 的自动调用对于 package_name:

Similarly, bindparam() is invoked automatically when working with CRUD statements as far as the “VALUES” portion is concerned.

它也适用于更新语句的 SET 部分。当试图执行它时,有一个非常具有描述性的异常:

In [9]: d = [{'package_name': 'something', 'trigger_service': 'somewhere'},
...: {'package_name': 'somesome', 'trigger_service': 'wherewhere'}]

In [10]: conn.execute(stmt, d)
---------------------------------------------------------------------------
CompileError Traceback (most recent call last)
...
CompileError: bindparam() name 'trigger_service' is reserved for
automatic usage in the VALUES or SET clause of this insert/update
statement. Please use a name other than column name when using
bindparam() with insert() or update() (for example, 'b_trigger_service').

所以答案就在异常本身:为 trigger_service bindparam() 使用不同的名称:

In [15]: stmt = t.update().\
...: where(t.c.trigger_service == bindparam('b_trigger_service'))

In [16]: d = [{'package_name': 'something', 'b_trigger_service': 'somewhere'},
...: {'package_name': 'somesome', 'b_trigger_service': 'wherewhere'}]

In [17]: conn.execute(stmt, d)
2017-08-09 22:03:12,823 INFO sqlalchemy.engine.base.Engine UPDATE demo_table SET package_name=? WHERE demo_table.trigger_service = ?
2017-08-09 22:03:12,823 INFO sqlalchemy.engine.base.Engine (('something', 'somewhere'), ('somesome', 'wherewhere'))
Out[17]: <sqlalchemy.engine.result.ResultProxy at 0x7fc391b0b748>

关于python - SQL Alchemy 使用带有 bindparam 的字典列表更新表不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45595562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com