473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Python sqlite and regex.

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

May 19 '06 #1
12 8470
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])") and the error is: cur.execute('select foo from test where foo regex tata')
apsw.SQLError: SQLError: near "regex": syntax error


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
May 19 '06 #2
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

May 19 '06 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Matt Good wrote:
SQLite3 already has a REGEXP function, so you don't need to create your
own. [...]


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-----
May 19 '06 #4
"Gerhard Häring" <gh@ghaering.de> wrote in message
news:ma***************************************@pyt hon.org...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Matt Good wrote:
SQLite3 already has a REGEXP function, so you don't need to create your
own. [...]


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.
"""


This is very interesting. So I *could* define my own regexp function that
processes not regular expressions, but say, glob-like strings, which are
usually much easier for end users to work with (very basic wild-carding
where '*' matches one or more characters, and '?' matches any single
character - maybe add '#' to match any single digit and '@' to match any
single alpha character).

-- Paul
May 19 '06 #5
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote in message
news:hu*******************@tornado.texas.rr.com...
where '*' matches one or more characters, and '?' matches any single


oops, I meant '*' matches zero or more characters.

In many applications, these tests are sufficient for most user queries. And
this eliminates the problem of teaching application users how to create
queries using the full regular expression syntax.

-- Paul

May 19 '06 #6
Paul McGuire wrote:
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote in message
news:hu*******************@tornado.texas.rr.com...
where '*' matches one or more characters, and '?' matches any single


oops, I meant '*' matches zero or more characters.


'?' also matches 0 characters
May 19 '06 #7
"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:4A******************@news.tufts.edu...
Paul McGuire wrote:
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote in message
news:hu*******************@tornado.texas.rr.com...
where '*' matches one or more characters, and '?' matches any single


oops, I meant '*' matches zero or more characters.


'?' also matches 0 characters


Maybe it does, and maybe it doesn't... :)

Here is my test with the glob module (in my directory, there is a file named
PUTTY.RND):
glob.glob('PUTTY.RND') ['PUTTY.RND'] glob.glob('PUTTY.RN?') ['PUTTY.RND'] glob.glob('PUTTY.RN?D') [] glob.glob('PUTTY.RN?') ['PUTTY.RND'] glob.glob('PUTTY.RND?') [] glob.glob('PUTT?.RND') ['PUTTY.RND'] glob.glob('PUTTY?.RND') [] glob.glob('PUTT?Y.RND') []


Looks like '?' does *not* match zero characters in glob. On the other hand,
in the Windows console window, it appears that '?' *does* match zero
characters.

Of course, you could write your regexp() routine to interpret '?' any way
you wanted.

-- Paul
May 19 '06 #8
On Fri, 19 May 2006 17:44:45 GMT,
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote:
"Gerhard Häring" <gh@ghaering.de> wrote in message
news:ma***************************************@pyt hon.org...
"""
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.
"""

This is very interesting. So I *could* define my own regexp function
that processes not regular expressions, but say, glob-like strings,
which are usually much easier for end users to work with (very basic
wild-carding where '*' matches one or more characters, and '?' matches
any single character - maybe add '#' to match any single digit and '@'
to match any single alpha character).


Doesn't SQL already have lightweight wildcards?

SELECT somefield FROM sometable WHERE someotherfield LIKE '%foo%'

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
May 19 '06 #9

"Dan Sommers" <me@privacy.net> wrote in message
news:m2************@unique.fqdn...
On Fri, 19 May 2006 17:44:45 GMT,
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote:
"Gerhard Häring" <gh@ghaering.de> wrote in message
news:ma***************************************@pyt hon.org...

"""
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.
"""

This is very interesting. So I *could* define my own regexp function
that processes not regular expressions, but say, glob-like strings,
which are usually much easier for end users to work with (very basic
wild-carding where '*' matches one or more characters, and '?' matches
any single character - maybe add '#' to match any single digit and '@'
to match any single alpha character).


Doesn't SQL already have lightweight wildcards?

SELECT somefield FROM sometable WHERE someotherfield LIKE '%foo%'


Yes it does - '%' is like '*', and '_' is like '?'. But it is rare for
application users to be familiar with these. In my experience, users are
more likely to have seen '*' and '?'.

The reason I keep citing end/application users, as opposed to developers, is
for the case where the user has filled in some kind of wildcard search
field, to be passed to SQL in a query. Woe to the app designer who figures
that users want to enter a regular expression in such a field. '%' and '_'
wildcards a little better, but frankly, I think '*' and '?' looks more like
a wildcards than '%' and '_'.

-- Paul
May 19 '06 #10
Oops, sorry about the confusion regarding the built-in REGEXP. That's
kind of disappointing. It would appear that the user-defined regexp
function in the original post should work assuming the SQL and regex
syntax errors are corrected.

However, there *is* a GLOB built-in to SQLite 3 that has a default
registered implementation (though it can be overriden by a user-defined
"glob" method if necessary).

May 19 '06 #11
On Fri, 19 May 2006 18:52:38 GMT,
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote:
"Dan Sommers" <me@privacy.net> wrote in message
news:m2************@unique.fqdn...
Doesn't SQL already have lightweight wildcards?

SELECT somefield FROM sometable WHERE someotherfield LIKE '%foo%'

Yes it does - '%' is like '*', and '_' is like '?'. But it is rare
for application users to be familiar with these. In my experience,
users are more likely to have seen '*' and '?'. The reason I keep citing end/application users, as opposed to
developers ...
Users, application users, end users, etc., vary over time. Since this
is c.l.p., though, we'd better just agree to disagree over which user(s)
may or may not be familiar with which (or any) wildcard conventions.
... the case where the user has filled in some kind of wildcard search
field, to be passed to SQL in a query. Woe to the app designer who
figures that users want to enter a regular expression in such a field
...
Woe to the app designer who fails to target the app to the users, make
it clear to the users what is expected in any given field, and accept
"constructive criticism" from unhappy users. ;-)

Knowing your users is one of the most important factors in software
design.
... '%' and '_' wildcards a little better, but frankly, I think '*'
and '?' looks more like a wildcards than '%' and '_'.


Ah, but then you're not an end user, are you? ;-)

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
May 19 '06 #12
John Salerno <jo******@NOSPAMgmail.com> writes:
Paul McGuire wrote:
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote in message
news:hu*******************@tornado.texas.rr.com...
where '*' matches one or more characters, and '?' matches any
single


oops, I meant '*' matches zero or more characters.


'?' also matches 0 characters


Not in globs. In a glob, '?' matches any one character, '*' matches
any zero or more characters.

In a regex, '.' matches any one character, '?' matches the preceding
atom zero or one times, '*' matches the preceding atom zero or more
times, and '+' matches the preceding atom one or more times.

They're quite different syntaxes, but confusingly similar in
appearance.

On most GNU+Linux systems, these two commands get the relevant manual
pages:

$ man 7 glob
$ man 7 regex

--
\ "When I get real bored, I like to drive downtown and get a |
`\ great parking spot, then sit in my car and count how many |
_o__) people ask me if I'm leaving." -- Steven Wright |
Ben Finney

May 20 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Gerhard Haering | last post by:
Last December, we had a short thread discussing (in python-dev) the integration of PySQLite into Python 2.4. At the time, I was against inclusion, because I thought PySQLite was not ripe for it,...
3
by: Michael Goettsche | last post by:
Hello guys, I succeeded in convincing my CS teacher to use Python and Sqlite instead of Microsoft Access to get started with databases. We are working on a windows terminal server to which I...
12
by: John Salerno | last post by:
I've been looking around and reading, and I have a few more questions about SQLite in particular, as it relates to Python. 1. What is the current module to use for sqlite? sqlite3? or is that not...
1
by: EuGeNe Van den Bulke | last post by:
Hi, Is the sqlite distributed with Python 2.5 compiled with the -DTHREADSAFE=1 flag? My gutt feeling is Windows (yes) MacOS/Linux (no) but ... If it is not on MacOS/Linux, how do I go about...
3
by: ricardo.turpino | last post by:
Hi, I've installed Mac Python 2.5. I'm running Mac OS X 10.4.10 on a Macbook 1.83GHz. I though that the python sqlite library was installed by default as part of Mac Python 2.5, however, I...
0
by: kurtf | last post by:
Hope somebody could help me with the creation of a table in sqlite3 from python. I am developing an application that allows a user to import a data file and insert the data into a sqlite3...
3
by: Daniel Fetchinson | last post by:
Does Python 2.5.2's embedded SQLite support full text searching? Sqlite itself is not distributed with python. Only a python db api compliant wrapper is part of the python stdlib and as such it...
2
by: Jean-Paul Calderone | last post by:
On Mon, 16 Jun 2008 08:39:52 +1000, Ben Finney <bignose+hates-spam@benfinney.id.auwrote: Maybe. I'm no expert on Debian packaging. However, exarkun@boson:~$ ls -l...
20
by: timotoole | last post by:
Hi all, On a (sun) webserver that I use, there is python 2.5.1 installed. I'd like to use sqlite3 with this, however sqlite3 is not installed on the webserver. If I were able to compile sqlite...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.