Answer the question
In order to leave comments, you need to log in
How to compare with substring without sql injection?
The request is formed in the python module.
Database - PostgreSQL.
There is a comparison with a substring in the sql query:
'''
SELECT *
FROM TableTemp
WHERE "SomeColumn" LIKE '%{0}%'
'''.format(<some_string>)
%' --
Answer the question
In order to leave comments, you need to log in
initd.org/psycopg/docs/usage.html#query-parameters
Psycopg casts Python variables to SQL literals by type. Many standard Python types are already adapted to the correct SQL representation.
Example: the Python function call:
>>> cur.execute(
... """INSERT INTO some_table (an_int, a_date, a_string)
... VALUES (%s, %s, %s);""",
... (10, datetime.date(2005, 11, 18), "O'Reilly"))
is converted into the SQL command:
INSERT INTO some_table (an_int, a_date, a_string)
VALUES (10, '2005-11-18', 'O''Reilly');
>>> cur.execute(
... """INSERT INTO some_table (an_int, a_date, another_date, a_string)
... VALUES (%(int)s, %(date)s, %(date)s, %(str)s);""",
... {'int': 10, 'str': "O'Reilly", 'date': datetime.date(2005, 11, 18)})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question