Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python3 sqlite和正则表达式查询_Python_Regex_Sqlite - Fatal编程技术网

python3 sqlite和正则表达式查询

python3 sqlite和正则表达式查询,python,regex,sqlite,Python,Regex,Sqlite,我使用python3在sqlite3数据库中搜索精确的单词。当我使用SQLite数据库浏览器时,它工作得非常好。但当我使用python3时,它抛出: sqlite3.OperationalError:用户定义函数引发异常。(我将使用此代码在树状视图中搜索应用程序) 代码如下: import sqlite3 import re def regexp(expr, item): reg = re.compile(expr) return reg.search(item) is no

我使用python3在sqlite3数据库中搜索精确的单词。当我使用SQLite数据库浏览器时,它工作得非常好。但当我使用python3时,它抛出: sqlite3.OperationalError:用户定义函数引发异常。(我将使用此代码在树状视图中搜索应用程序)

代码如下:

import sqlite3
import re


def regexp(expr, item):
    reg = re.compile(expr)
    return reg.search(item) is not None
conn = sqlite3.connect('/database.db')
conn.create_function("REGEXP", 2, regexp)
cursor = conn.cursor()
cursor.execute('SELECT * FROM table WHERE column REGEXP ?',['(?i)(?<=\s|^|\W)word(?=\s|$|\W)'])
data=cursor.fetchall()
print(data)
导入sqlite3 进口稀土 def regexp(expr,项目): reg=重新编译(expr) 返回注册搜索(项目)不是无 conn=sqlite3.connect('/database.db') conn.create_函数(“REGEXP”,2,REGEXP) 游标=连接游标()
cursor.execute('SELECT*FROM table WHERE column REGEXP?',['(?)i)(?The
(?)i)(?您是否尝试将create_函数中的第一个参数以小写形式写入“regex”?是的,但它不起作用…请参阅我的编辑程序非常感谢您的帮助!现在,在我替换它之后,它会向我抛出一个新错误“expected string或bytes like object”好的。但是如何解决这个问题呢?这与python有关,因为查询是在sqlite上工作的browser@Béa请参阅
regexp
方法的更新代码。谢谢你。但我仍然收到“预期的字符串或类似字节的对象”…在SQLte中,列是文本类型,所以我不太理解。非常感谢你!它工作得很好!!非常高兴
import sqlite3
import re
def regexp
def regexp(expr, item):
    try:
        pattern = re.compile(expr, re.I)

        return re.search(pattern, str) is not None
    except Exception as e:
        print(e)


conn = sqlite3.connect('database.db')
conn.create_function("regexp", 2, regexp)
cursor = conn.cursor()
try:
    cursor.execute(r'SELECT * FROM table WHERE column REGEXP ?',[r'(?i)(?<=\s|^|\W)word(?=\s|$|\W)'])
    data=cursor.fetchall()
    print(data)
except Exception as e:
    print(e)