mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-23 01:16:34 +03:00
Added note about backslashes and LIKE
Added note about the use of LIKE with strings containing backslashes. Addresses concern in issue #785.
This commit is contained in:
parent
9d83b03605
commit
67b94d0797
|
@ -221,6 +221,27 @@ argument of the `~cursor.execute()` method::
|
||||||
>>> cur.execute(SQL, data) # Note: no % operator
|
>>> cur.execute(SQL, data) # Note: no % operator
|
||||||
|
|
||||||
|
|
||||||
|
Values containing backslashes and LIKE
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
Unlike in Python, the **backslash** (`\`) is not used as an escape
|
||||||
|
character *except* in patterns used with `LIKE` and `ILIKE` where they
|
||||||
|
are needed to escape the `%` and `_` characters.
|
||||||
|
|
||||||
|
This can lead to confusing situations::
|
||||||
|
|
||||||
|
>>> path = r'C:\Users\Bobby.Tables'
|
||||||
|
>>> cur.execute('INSERT INTO mytable(path) VALUES (%s)', (path,))
|
||||||
|
>>> cur.execute('SELECT * FROM mytable WHERE path LIKE %s', (path,))
|
||||||
|
>>> cur.fetchall()
|
||||||
|
[]
|
||||||
|
|
||||||
|
The solution is to specify an `ESCAPE` character of `''` (empty string)
|
||||||
|
in your `LIKE` query::
|
||||||
|
|
||||||
|
>>> cur.execute("SELECT * FROM mytable WHERE path LIKE %s ESCAPE ''", (path,))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
.. index::
|
.. index::
|
||||||
single: Adaptation
|
single: Adaptation
|
||||||
|
|
Loading…
Reference in New Issue
Block a user