0

its possible to use variables on filters?

My actual code:

import sqlalchemy
from sqlalchemy import Table, MetaData, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base


location = 'localhost'
engine_str = 'mysql+mysqlconnector://xxx:xxx@xxx/xxx'.format(location)
engine = sqlalchemy.create_engine(engine_str, echo=False)
session = sessionmaker(bind=engine)
connection = engine.connect()
session = session(bind=connection)
metadata = MetaData()
Base = declarative_base()

class Services(Base):
    """
    Service details on database
    """
    __tablename__ = 'some_table'
    id = Column(Integer, primary_key=True)
    color= Column(String)
    shape= Column(String)

    def __repr__(self):
        return self.shape

This works:

for c in session.query(Services).filter(Services.name.in_(['blue','red'])):
    print(c)

result:

    circle, square

But using avariable, returns:

services = ('blue','red')
for c in session.query(Services).filter(Services.name.in_([services])):
    print(c)

result:

mysql.connector.errors.ProgrammingError: Failed processing pyformat-parameters; 'MySQLConverter' object has no attribute '_tuple_to_mysql'

Thanks in advance!

4
  • Your last sample code is not actually using services. What is the actual code you are using? Commented Mar 10, 2014 at 12:50
  • @MartijnPieters hi, what you mean? Commented Mar 10, 2014 at 12:51
  • Your last sample of code uses the exact same for loop as the other sample. services is not used in that loop. Commented Mar 10, 2014 at 12:54
  • @MartijnPieters, Ah, thanks for pointing that out, bad copy/paste. Commented Mar 10, 2014 at 12:55

1 Answer 1

2

services is already a sequence, no need to wrap it in a list:

for c in session.query(Services).filter(Services.name.in_(services)):
Sign up to request clarification or add additional context in comments.

1 Comment

Oh geez... cant believe it was that... Martijin, i cant count the times you saved me from overnight bad code development. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.