Python sqlite和regex。 [英] Python sqlite and regex.
问题描述
我想在sqlite查询中使用正则表达式,我使用apsw模块
但是它不起作用...你能不能帮助我?
我的剧本:
导入apsw
导入重新
path =''db / db.db3''
#regexp函数(从python-list discusion中提取)
def regexp(expr,item):
reg = re.compile(expr)
返回reg.match(item)不是没有
con = apsw。连接(路径)
#create函数
con.createscalarfunction(" REGEXP",regexp)
cur = con.cursor()
#exampl
cur.execute(" select foo from test where foo regex''aa。[0-9])")
>
,错误是:
cur.execute(''从测试中选择foo foo regex tata'')
apsw.SQLError :SQLError:接近正则表达式:语法错误
谢谢
Hi,
I''d like to use regular expressions in sqlite query, I using apsw module
but it doesn''t work...Can you help me ?
My script:
import apsw
import re
path = ''db/db.db3''
#regexp function (extract from python-list discusion)
def regexp(expr, item):
reg = re.compile(expr)
return reg.match(item) is not None
con = apsw.Connection(path)
#create function
con.createscalarfunction("REGEXP", regexp)
cur = con.cursor()
#exampl
cur.execute("select foo from test where foo regex ''aa.[0-9])")
and the error is:
cur.execute(''select foo from test where foo regex tata'')
apsw.SQLError: SQLError: near "regex": syntax error
Thanks
推荐答案
2006年5月19日星期五14:47:10 +0200,
Julien ARNOUX< ju *********** @ ext.cri74。有机>写道:
On Fri, 19 May 2006 14:47:10 +0200,
Julien ARNOUX <ju***********@ext.cri74.org> wrote:
cur.execute(" select foo from test where foo regex''aa。[0-9])")
,错误是:
cur.execute(''从测试中选择foo foo regex tata'')
apsw.SQLError:SQLError:nearregex:语法错误
cur.execute("select foo from test where foo regex ''aa.[0-9])") and the error is: cur.execute(''select foo from test where foo regex tata'')
apsw.SQLError: SQLError: near "regex": syntax error
我想你错过了关于该正则表达式的收盘报价;或者那个'
最后是一个额外的右括号。
此外,最好让数据库模块进行任何转义你可能需要
。例如:
fooregex = r''aa。[0-9]''
sql =''从test中选择foo foo regex%s ''
cur.execute(sql,tuple(fooregex))
有关详细信息,请参阅DP API规范。
问候,
Dan
-
Dan Sommers
< http ://www.tombstonezero.net/dan/>
我希望人们会按字母顺序死亡。 - 我的妻子,系谱学家
I think you''re missing a closing quote on that regex; or perhaps that''s
an extra closing parenthesis at the end.
Also, it''s probably best to let the database module do any escaping you
may need. For example:
fooregex = r''aa.[0-9]''
sql = ''select foo from test where foo regex %s''
cur.execute( sql, tuple( fooregex ) )
See the DP API spec for more information.
Regards,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
SQLite3已经有了REGEXP功能,所以你不需要创建你自己的
。 br />
正如丹提到你的表达方式也有问题:''aa。[0-9])
你需要一个关于表达式的结束语,并且你需要将
关闭的paren与开放的paren匹配,或者将其移除。
另外,如果你不知道,将会是一个sqlite3模块在
Python 2.5基于pysqlite 2.2: http:// initd .org / tracker / pysqlite
如果重要的话,使用pysqlite可以更容易地转移到Python 2.5 sqlite3
模块给你。
- 马特好
SQLite3 already has a REGEXP function, so you don''t need to create your
own.
As Dan mentioned you also have a problem in your expression: ''aa.[0-9])
You need a closing quote on the expression, and you need to match the
close paren with an open paren, or remove it.
Also, in case you weren''t aware, there will be a "sqlite3" module in
Python 2.5 based on pysqlite 2.2: http://initd.org/tracker/pysqlite
Using pysqlite will make it easier to move to the Python 2.5 sqlite3
module if that''s important to you.
-- Matt Good
-----开始PGP签名留言--- -
哈希:SHA1
Matt Good写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Matt Good wrote:
SQLite3已经有了REGEXP函数,所以你不要需要创建自己的。 [...]
SQLite3 already has a REGEXP function, so you don''t need to create your
own. [...]
是的,但SQLite不包含正则表达式引擎,因此根据您需要的SQLite文档
在
命令中注册REGEXP函数以使REGEXP操作符工作:
"""
REGEXP operator是regexp()用户函数的特殊语法。否
regexp()用户函数默认定义,因此使用REGEXP
运算符通常会产生错误消息。如果用户定义的
函数名为regexp,那么在运行时定义,该函数将被调用以实现REGEXP运算符。
"""
- - Gerhard
----- BEGIN PGP SIGNATURE -----
版本:GnuPG v1.4.1(GNU / Linux)
评论:将GnuPG与Thunderbird一起使用 - http://enigmail.mozdev.org
iD8DBQFEbfbQdIO4ozGCH14RAqqqAJ49 / 9Kpi8xA6AyGB0tVJ / JcU4MczgCgoIsW
gdYgUl9ge63CiHqj4Mzgpns =
= 1ZDV
--- --END PGP SIGNATURE -----
Yes, but SQLite does not include a regular expression engine, and thus
according to the SQLite docs you need to register a REGEXP function in
order to make the REGEXP operator work:
"""
The REGEXP operator is a special syntax for the regexp() user function. No
regexp() user function is defined by default and so use of the REGEXP
operator will normally result in an error message. If a user-defined
function named "regexp" is defined at run-time, that function will be
called in order to implement the REGEXP operator.
"""
- -- Gerhard
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFEbfbQdIO4ozGCH14RAqqqAJ49/9Kpi8xA6AyGB0tVJ/JcU4MczgCgoIsW
gdYgUl9ge63CiHqj4Mzgpns=
=1ZDV
-----END PGP SIGNATURE-----
这篇关于Python sqlite和regex。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!