python 3.6。我正在尝试为SQLITE创建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',)]
文档说:
REGEXP操作员是Regexp((用户函数的特殊语法。…" X Regexp Y"运算符将作为" Regexp(Y,X("的呼叫实施。
您的功能必须有两个参数。