BritishGeologicalSurvey / etlhelper

ETL Helper is a Python ETL library to simplify data transfer into and out of databases.
https://britishgeologicalsurvey.github.io/etlhelper/
GNU Lesser General Public License v3.0
104 stars 25 forks source link

Character '%' cause an error #144

Closed LuiguiSaenz closed 1 year ago

LuiguiSaenz commented 1 year ago

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

volcan01010 commented 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
volcan01010 commented 1 year ago

Hi @LuiguiSaenz, do you have any more information that might help use track down the problem?

LuiguiSaenz commented 1 year ago

when you use a like in the query, for example: UPDATE TABLE SET FIELD1='value' WHERE CODIGO LIKE 'TN%'

volcan01010 commented 1 year ago

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
rbroth commented 1 year ago

Closing, because we can't reproduce the problem with the information provided