Closed LuiguiSaenz closed 1 year ago
That's strange. Can you provide an example? Are you using parameter substitution? What is the value of parameters
in your example?
I wonder if the %
is being interpreted as part of a parameter substitution? For PostgreSQL, %s
is used as a placeholder. Perhaps your %
is being interpreted as a placeholder and then the database is looking for a value that doesn't exist. See equivalent for executemany: https://github.com/BritishGeologicalSurvey/etlhelper#insert-rows
The parameters should be an container e.g. tuple or list. I sometimes get a tuple index exception if I forget that and pass in a raw string. e.g.
execute("INSERT INTO my_table (my_column) VALUES (%s)", "hello") # wrong
execute("INSERT INTO my_table (my_column) VALUES (%s)", ("hello",)) # correct
Hi @LuiguiSaenz, do you have any more information that might help use track down the problem?
when you use a like in the query, for example: UPDATE TABLE SET FIELD1='value' WHERE CODIGO LIKE 'TN%'
Can you give more information please, e.g. the full execute
command that you are using and some example values for query
and parameters
that cause the problem? I am not able to recreate your issue.
I wondered if the issue was with the logging system, as it can interpret %
characters as placeholders. But I ran your query against the logging that runs in etlhelper.execute and it seems to work:
>>> import logging
>>> logger = logging.getLogger()
>>> logging.basicConfig(level=logging.DEBUG)
>>> query = "UPDATE TABLE SET FIELD 1='value' WHERE CODIGO LIKE 'TN%'"
>>> parameters = ()
>>> conn = "my connection"
>>> logger.debug(f"Executing:\n\n{query}\n\nwith parameters:\n\n"
... f"{parameters}\n\nagainst\n\n{conn}")
>>> logger.debug(f"Executing:\n\n{query}\n\nwith parameters:\n\n"
... f"{parameters}\n\nagainst\n\n{conn}")
DEBUG:root:Executing:
UPDATE TABLE SET FIELD 1='value' WHERE CODIGO LIKE 'TN%'
with parameters:
()
against
my connection
Closing, because we can't reproduce the problem with the information provided
When the % character exists in the query to be executed as part of a string, the "execute" function throws the following error
File "/home/luigui/Escritorio/repositorios/logros-backend/env/lib/python3.8/site-packages/etlhelper/etl.py", line 429, in execute cursor.execute(query, parameters) IndexError: tuple index out of range