我想在sqlite中使用正则表达式,但我不知道如何.
我的表有一个字符串,如下所示:"3,12,13,14,19,28,32"现在如果我输入"where x LIKE'3'"我也得到包含13或32之类值的行,但我想只获得该字符串中具有正好值3的行.
有谁知道如何解决这个问题?
我试图以...的形式执行查询
SELECT col2 FROM tab WHERE col1 IN (val1, val2, val3...)
Run Code Online (Sandbox Code Playgroud)
...值存储在任意长度的Python列表/元组中.我似乎无法找到一种"干净"的方式来做到这一点.
>>> db = connect(":memory:")
>>> db.execute("CREATE TABLE tab (col1 INTEGER, col2 TEXT)")
>>> db.execute("INSERT INTO tab VALUES(1,'one')")
>>> db.execute("INSERT INTO tab VALUES(2,'two')")
>>> db.execute("INSERT INTO tab VALUES(3,'three')")
>>> db.execute("INSERT INTO tab VALUES(4,'four')")
>>> db.execute("INSERT INTO tab VALUES(5,'five')")
>>> db.commit()
# Expected result
>>> db.execute("SELECT col2 FROM tab WHERE col1 IN (1,3,4)").fetchall()
[(u'one',), (u'three',), (u'four',)]
>>> vals = (1,3,4)
>>> db.execute("SELECT col2 FROM tab WHERE col1 IN (?)", vals).fetchall() …Run Code Online (Sandbox Code Playgroud) 我尝试在sqlite数据库上使用带有python的正则表达式检查带有模式的字符串.当我尝试使用"使用patern使用"搜索字符串时,我有问题例如:
cur.execute("insert into articles(id,subject) values (1,'aaa\"test\"')")
cur.execute("select id,subject from articles where id = 1")
print (cur.fetchall())
cur.execute("select subject from articles where subject regexp '\"test\"' ")
print (cur.fetchall())
Run Code Online (Sandbox Code Playgroud)
我应该"在regexp之前其他方式编译器不喜欢...语法错误
[(1, 'aaa"test"')]
[] <????? should found
Run Code Online (Sandbox Code Playgroud)
有人知道怎么做吗?
我的正则表达式函数:con.create_function("regexp",2,regexp)