python - sqlite3:用python创建函数正则表达式
问题描述
蟒蛇 3.6。我正在尝试为 sqlite3 创建一个 REGEXP 函数。我有错误:OperationalError: wrong number of arguments to function REGEXP()
这是我的代码:
import sqlite3
import re
def fonctionRegex(mot):
patternRecherche = re.compile(r"\b"+mot.lower()+"\\b")
return patternRecherche.search(item) is not None
dbName = 'bdd.db'
connexion = sqlite3.connect(dbName)
leCursor = connexion.cursor()
connexion.create_function("REGEXP", 1, fonctionRegex)
mot = 'trump'
data = leCursor.execute('SELECT * FROM tweet WHERE texte REGEXP ?',mot).fetchall()
谢谢
解决方案
你做错了什么。这是更正确的例子
import sqlite3
import re
def functionRegex(value, pattern):
c_pattern = re.compile(r"\b" + pattern.lower() + r"\b")
return c_pattern.search(value) is not None
connection = sqlite3.connect(':memory:')
cur = connection.cursor()
cur.execute('CREATE TABLE tweet(msg TEXT)')
cur.execute('INSERT INTO tweet VALUES("This is a test message")')
cur.execute('INSERT INTO tweet VALUES("Another message")')
connection.create_function("REGEXP", 2, functionRegex)
print(cur.execute('SELECT * FROM tweet WHERE REGEXP(msg, ?)', ('test',)).fetchall())
print(cur.execute('SELECT * FROM tweet WHERE REGEXP(msg, ?)', ('message',)).fetchall())
print(cur.execute('SELECT * FROM tweet WHERE ? REGEXP msg', ('message',)).fetchall())
将打印
[('This is a test message',)]
[('This is a test message',), ('Another message',)]
[('This is a test message',), ('Another message',)]
推荐阅读
- python - python 正则表达式:行尾的反向匹配
- python - Django,Next.JS:缺少 CORS 标头“Access-Control-Allow-Origin”,即使定义了 django-cors-headers,CORS 请求也未成功
- php - (PHP) 具有不同变量的相同程序
- java - 当我打开另一个活动寄存器时应用程序崩溃
- java - 反序列化对java中自定义枚举值的json响应
- java - 无法在 testng 中实例化类问题
- c# - 绑定不会出现在 XAML 上的标签上
- node.js - 如何使用 nodejs 查看 ssas 分析服务角色
- java - MongoCollection 中的搜索查询
- consul - Vitess:将参数传递给领事