I am trying to make an ilike query on a PostgreSQL database:
conn.execute('select geocodigo, nome, GEOJSON from "Dengue_global"."Municipio" where nome' " ilike('{}%')".format(m[:4]))
where m[:4]
is the substring I am trying to match.
I am getting the following Exception:
TypeError Traceback (most recent call last)
<ipython-input-38-72ff2adae1f7> in <module>
5 except Exception:
6 print(m)
----> 7 res = conn.execute('select geocodigo, nome, GEOJSON from "Dengue_global"."Municipio" where nome' r" ilike('{}%')".format(m[:4]))
8 gj = res.fetchone()
9 if gj is None:
/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py in execute(self, object_, *multiparams, **params)
972 """
973 if isinstance(object_, util.string_types[0]):
--> 974 return self._execute_text(object_, multiparams, params)
975 try:
976 meth = object_._execute_on_connection
/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py in _execute_text(self, statement, multiparams, params)
1145 parameters,
1146 statement,
-> 1147 parameters,
1148 )
1149 if self._has_events or self.engine._has_events:
/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
1238 except BaseException as e:
1239 self._handle_dbapi_exception(
-> 1240 e, statement, parameters, cursor, context
1241 )
1242
/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py in _handle_dbapi_exception(self, e, statement, parameters, cursor, context)
1458 util.raise_from_cause(sqlalchemy_exception, exc_info)
1459 else:
-> 1460 util.reraise(*exc_info)
1461
1462 finally:
/usr/local/lib/python3.6/dist-packages/sqlalchemy/util/compat.py in reraise(tp, value, tb, cause)
275 if value.__traceback__ is not tb:
276 raise value.with_traceback(tb)
--> 277 raise value
278
279
/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
1234 if not evt_handled:
1235 self.dialect.do_execute(
-> 1236 cursor, statement, parameters, context
1237 )
1238 except BaseException as e:
/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/default.py in do_execute(self, cursor, statement, parameters, context)
534
535 def do_execute(self, cursor, statement, parameters, context=None):
--> 536 cursor.execute(statement, parameters)
537
538 def do_execute_no_params(self, cursor, statement, context=None):
TypeError: 'dict' object does not support indexing
If I remove the %
symbol from the query the exception goes away, but my match does not work.
Any Ideas about what can be causing this and how to work around this and have my query work?
===== Solution =====
from sqlalchemy import text
conn.execute(atext('select geocodigo, nome, GEOJSON from "Dengue_global"."Municipio" where nome' " ilike(:search ||'%')"),
{'search': m[:4]})