[Python] How to properly escape strings when manually building SQL queries in SQLAlchemy?

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am using SQLAlchemy to connect to different databases in Python, but not with the ORM support as this cannot be implemented due to several reasons.

    Mainly I do build a complex SQL query using things like

    sql += "AND fieldname = '%s'" % myvar


    In my case is not a problem of SQL injection as the data is always from a trusted source but even if the source is trusted it could contain characters that could break the query like ', % or _.

    Mainly, I need to escape them, and I wonder if there is an already existing escape function that I could re-use.

    Continue reading...

Compartilhe esta Página