Dec-01-2021, 08:27 PM
Hi
Im trying to do a regex search on an sqlite database where the expressions are held in a list. I have the connection working as shown by the first example in the code below but when I try to use the second example it fails. I presume its to do with formatting of the expression. The error is shown underneath.
Id be very grateful for any help as I cant see the problem
================== RESTART: D:\Python\Wordfeud\Forumpost.py ==================
('APPARENCIES',)
Traceback (most recent call last):
File "D:\Python\Wordfeud\Forumpost.py", line 26, in <module>
cursor.execute(sqltxt)
sqlite3.OperationalError: near "'SELECT Word FROM Tbwords WHERE word REGEXP ?'": syntax error
>>>
Im trying to do a regex search on an sqlite database where the expressions are held in a list. I have the connection working as shown by the first example in the code below but when I try to use the second example it fails. I presume its to do with formatting of the expression. The error is shown underneath.
Id be very grateful for any help as I cant see the problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import sqlite3, re def regexp(expr, item): reg = re. compile (expr) return reg.search(item) is not None database = "d:/Python/Wordfeud/Dictionary.db" conn = sqlite3.connect(database) conn.create_function( "REGEXP" , 2 , regexp) cursor = conn.cursor() #This works cursor.execute( 'SELECT Word FROM Tbwords WHERE word REGEXP ?' ,[ '^.?.?P.?.?.?RE.?.?C.?.?.' ]) data = cursor.fetchone() print (data) #This doesnt list = [ '.?.?P.?.?.?RE.?.?C.?.?.' ] regex = list [ 0 ] re_end = "$']" #end of re mask re_start = "['^" #beginning of re mask regex = re_start + regex + re_end sqltxt = "'SELECT Word FROM Tbwords WHERE word REGEXP ?'," + regex cursor.execute(sqltxt) data = cursor.fetchone() print (data) |
('APPARENCIES',)
Traceback (most recent call last):
File "D:\Python\Wordfeud\Forumpost.py", line 26, in <module>
cursor.execute(sqltxt)
sqlite3.OperationalError: near "'SELECT Word FROM Tbwords WHERE word REGEXP ?'": syntax error
>>>
Attached Files