Select with LIKE operator using an array

Asked

Viewed 126 times

1

I am trying to select with LIKE operator using an array, but it is returning error when forming the string.

SCRIPT

placeholder= '?'
placeholders= ', '.join(placeholder for unused in NAMES)
query= "select * from DADOS where CLIENTNAME like %%s%" % placeholders 
ibm_db_cur.execute(query, NAMES)
resultFinal = ibm_db_cur.fetchall()

for linha in resultFinal:
        print(linha)

OUTPUT

Valueerror: incomplete format

API BD

import ibm_db
import ibm_db_dbi

ARRAY

NAMES = ['client1','client2','client3']

1 answer

3


Friend, the problem is that you put the handle %s amid %, so python gets lost when replacing.
Change your code to:

query= "select * from DADOS where CLIENTNAME like %%s%".format(placeholders)

Note that the select output you are mounting will be:

select * from DADOS where CLIENTNAME like %?, ?, ?%
  • Whoa, that was it, buddy, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.