python sqlite3 支持REGEXP语句 正则表达式 regular expression

 SQlite3是支持REGEXP语句的,但是python下运行sqlite3,却说解析“REGEXP”失败,如何让python下可能使用REGEXP呢?

import sqlite3
import re

def regexp(expr, item):
    reg = re.compile(expr)
    return reg.search(item) is not None

conn = sqlite3.connect(':memory:')
conn.create_function("REGEXP", 2, regexp)
cursor = conn.cursor()
cursor.execute('CREATE TABLE foo (bar TEXT)')
cursor.executemany('INSERT INTO foo (bar) VALUES (?)',[('aaa"test"',),('blah',)])
cursor.execute('SELECT bar FROM foo WHERE bar REGEXP ?',['"test"'])
data=cursor.fetchall()
print(data)

参考:https://stackoverflow.com/questions/5365451/problem-with-regexp-python-and-sqlite

posted @ 2020-10-09 11:52  重积德  阅读(1791)  评论(0编辑  收藏  举报