SQL Alchemy update table using list of dicts with bindparam not working

Viewed 1935

I'm trying to perform an update based on a list of dictionaries using bindparam, but I'm not sure why this example isn't working:

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)

EDIT

It seems the problem is when using bindparam here, because when i force the value it works perfectly. Still trying to understand why isn't working.

t.c.trigger_service == bindparam('trigger_service')
1 Answers
Related