Home > Net >  SQLAlchemy pass parameters with like '%STRING%' in raw sql
SQLAlchemy pass parameters with like '%STRING%' in raw sql

Time:01-28

I have a sql:

select *
from my table
where exists (
    select
    from unnest(procedure) elem
    where elem like '%String%'
          )

It works well when directly execute in database ide.

My question is how can I set 'String' as a parameters ?

I have tried:

String = 'hello world'

my_query = '''
    select *
    from my_table
    where exists (
        select
        from unnest(procedure) elem
        where elem like '% :String %'
              )
    '''

result=connection.execute(text(my_query),String=String)

However it returns nothing,I think maybe "like %" is special ?

Any friend can help ?

CodePudding user response:

I think a better solution is to put the percent signs in the variable itself, and have only :String in the query:

String = '%hello world%'
my_query = '''
    ...
    where elem like :String
'''
  • Related