From 67b94d0797ffd18c74cd1edac72a23918629694b Mon Sep 17 00:00:00 2001 From: Alan D Moore Date: Mon, 8 Oct 2018 09:36:16 -0500 Subject: [PATCH 1/2] Added note about backslashes and LIKE Added note about the use of LIKE with strings containing backslashes. Addresses concern in issue #785. --- doc/src/usage.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/doc/src/usage.rst b/doc/src/usage.rst index db8674c1..74715a39 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 From 20647b7bcc3b0d26d40455e7c47edf328f7e6d95 Mon Sep 17 00:00:00 2001 From: Alan D Moore Date: Mon, 8 Oct 2018 09:40:51 -0500 Subject: [PATCH 2/2] Fix RST markup --- doc/src/usage.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/src/usage.rst b/doc/src/usage.rst index 74715a39..335ebf38 100644 --- a/doc/src/usage.rst +++ b/doc/src/usage.rst @@ -224,7 +224,7 @@ argument of the `~cursor.execute()` method:: Values containing backslashes and LIKE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Unlike in Python, the **backslash** (`\`) is not used as an escape +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.