3

I'd like to represent the following query in SQLAlchemy:

select * from table where bit_count(column & bitmask) > 5

Basically I'm looking to select any row with a certain count of flags set. However, SQLAlchemy doesn't seem to have defined the BIT_COUNT() function. Anyone know any tricks to make this query in SQLAlchemy?

0

1 Answer 1

3
>>> session.query("id", "name", "thenumber12").\
...         from_statement("SELECT id, name, 12 as "
...                 "thenumber12 FROM users where bit_count(column&bitmask)<:the_val").\
...                 params(the_val=5).all()

something like that I imagine...

or

>>> from sqlalchemy import create_engine
>>> engine = create_engine('sqlite:///:memory:', echo=True)
>>> engine.execute("select * from table where bit_count(column & bitmask) > 5").scalar()

keep in mind im operating under the assumption that this is the correct use of bit_count ...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that first form is what I ended up using. It seems to take advantage of SQLAlchemy the most.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.