SQL Alchemy更新表使用了bindparam不能正常工作的dicts列表

时间:2017-08-09 16:00:19

标签: python python-2.7 sqlalchemy

我尝试使用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')

1 个答案:

答案 0 :(得分:3)

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

的方式
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()并依赖于{em> package_name 的bindparam()的自动调用:

  

类似地,就“CRUES”部分而言,在处理CRUD语句时会自动调用bindparam()

它也适用于更新语句的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>