Anis Souames
Anis Souames

Reputation: 344

How can I select entries that have a string in them with SQLITE

I have an SQLite DB where I have a table which has a column called Support .

The support column has a list of values (A,B,C,D) however this list is a string not an array and the elements do not have a type.

I would like to select entries that have A in that list . But I couldn't use the IN operator with an inner query since it's a string and not an array .

I'm using SQLAlchemy, and it's docs says that MATCH doesn't work when using SQLite as backend, so How can I proceed ?

Upvotes: 0

Views: 134

Answers (1)

ACV
ACV

Reputation: 1985

If I understand your question you have a column called Support with strings like A,B,C,D and you want to select all the rows where the "list" of elements in Support contains A.

My first thought is you could use a wild card in your select statement. So in SQL that would be:

SELECT * FROM my_table WHERE Support LIKE '%A%';

In SQLAlchemy this would be something like:

session.query(MyTable).filter(MyTable.Support.like('%A%')).all()

Alternatively, you could just fetch all the data and filter the list in memory in Python. But, generally I think it's better to perform the filtering at the database level.

Upvotes: 1

Related Questions