diff options
author | Alan D Moore <me@alandmoore.com> | 2018-10-08 09:36:16 -0500 |
---|---|---|
committer | Alan D Moore <me@alandmoore.com> | 2018-10-08 09:36:16 -0500 |
commit | 67b94d0797ffd18c74cd1edac72a23918629694b (patch) | |
tree | 18669c69b1c153dfe70c60ac8a20ecc991f7bda6 | |
parent | 9d83b036059d88b49477d5a068b4fad84076de30 (diff) | |
download | psycopg2-67b94d0797ffd18c74cd1edac72a23918629694b.tar.gz |
Added note about backslashes and LIKE
Added note about the use of LIKE with strings containing
backslashes. Addresses concern in issue #785.
-rw-r--r-- | doc/src/usage.rst | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/doc/src/usage.rst b/doc/src/usage.rst index db8674c..74715a3 100644 --- a/doc/src/usage.rst +++ b/doc/src/usage.rst @@ -221,7 +221,28 @@ argument of the `~cursor.execute()` method:: >>> 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:: single: Adaptation pair: Objects; Adaptation |