I try to check a string with a pattern using a regex with python on a sqlite database. I have problem when I try de search string having " with a patern using " For exemple:
cur.execute("insert into articles(id,subject) values (1,'aaa\"test\"')")
cur.execute("select id,subject from articles where id = 1")
print (cur.fetchall())
cur.execute("select subject from articles where subject regexp '\"test\"' ")
print (cur.fetchall())
I should \" before regexp other way compiler dont like... syntaxe error
[(1, 'aaa"test"')]
[] <????? should found
Somebody know how to do that ?
My regexp function :con.create_function("regexp", 2, regexp)
Another parameterized query example...
For the situation where you have to supply your own REGEX function to the database - I guess Python sqlite3 doesn't always have it set up by default.
In another example, the custom REGEX function is compiling the same expression for each and every match. There's a way around that. The example below also has a comment at the bottom of another way to define the REGEX operation.
You can get around compiling the expression every time the expression is used (for each match) for queries dealing with a lot of data by creating a custom function for each query and compiling the expression only once. Below self._conn is the database connection, and curs is a cursor from it.
Use parametrized sql. Then you don't need to escape the quotes yourself:
yields
You can use triple escapes, or a raw string.
Your doing:
Use a raw string, which is a
r'string with a r in front'
:Or triple escapes (
\\\
):