473,732 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.createscala rfunction("REGE XP", regexp)
cur = con.cursor()
#exampl
cur.execute("se lect foo from test where foo regex 'aa.[0-9])")

and the error is:

cur.execute('se lect foo from test where foo regex tata')
apsw.SQLError: SQLError: near "regex": syntax error

Thanks

May 19 '06 #1
12 8546
On Fri, 19 May 2006 14:47:10 +0200,
Julien ARNOUX <ju***********@ ext.cri74.org> wrote:
cur.execute("se lect foo from test where foo regex 'aa.[0-9])") and the error is: cur.execute('se lect 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.tombstoneze ro.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

iD8DBQFEbfbQdIO 4ozGCH14RAqqqAJ 49/9Kpi8xA6AyGB0tV J/JcU4MczgCgoIsW
gdYgUl9ge63CiHq j4Mzgpns=
=1ZDV
-----END PGP SIGNATURE-----
May 19 '06 #4
"Gerhard Häring" <gh@ghaering.de > wrote in message
news:ma******** *************** *************** *@python.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.r r._bogus_.com> wrote in message
news:hu******** ***********@tor nado.texas.rr.c om...
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.r r._bogus_.com> wrote in message
news:hu******** ***********@tor nado.texas.rr.c om...
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******@NOSPA Mgmail.com> wrote in message
news:4A******** **********@news .tufts.edu...
Paul McGuire wrote:
"Paul McGuire" <pt***@austin.r r._bogus_.com> wrote in message
news:hu******** ***********@tor nado.texas.rr.c om...
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('PUTT Y.RND') ['PUTTY.RND'] glob.glob('PUTT Y.RN?') ['PUTTY.RND'] glob.glob('PUTT Y.RN?D') [] glob.glob('PUTT Y.RN?') ['PUTTY.RND'] glob.glob('PUTT Y.RND?') [] glob.glob('PUTT ?.RND') ['PUTTY.RND'] glob.glob('PUTT Y?.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.r r._bogus_.com> wrote:
"Gerhard Häring" <gh@ghaering.de > wrote in message
news:ma******** *************** *************** *@python.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.tombstoneze ro.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.fqd n...
On Fri, 19 May 2006 17:44:45 GMT,
"Paul McGuire" <pt***@austin.r r._bogus_.com> wrote:
"Gerhard Häring" <gh@ghaering.de > wrote in message
news:ma******** *************** *************** *@python.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

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

Similar topics

11
1503
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, mostly because I thought the API was not stable. Now, I have started writing a new PySQLite module, which has the following key features: - Uses iterator-style SQLite 3.x API: sqlite3_compile, sqlite3_step() etc. This way, it is possible to...
3
2416
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 have no admin access, so I'd like to ask you which module is best suited to use Sqlite with Python under windows. The best would be a module which is easy to install without further dependencies. Thanks in advance,
12
2485
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 out until Python 2.5? 2. What's the difference between sqlite and pysqlite? Do you need both, just one, or is one an older version of the same thing? 3. What's the difference between the command line program called sqlite3
1
2449
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 replacing the sqlite so file with a threadsafe sqlite? Thanks,
3
3600
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 still have a problem. Sqlite does not appear to be my system: Traceback (most recent call last):
0
1727
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 database. I want the names of the fields to be derived from the first row of the file. I have been trying to perform this with code like: # CODE BEGIN import sqlite3
3
275
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 is completely independent of the sqlite build. In other words, if your sqlite build supports full text searching you can use it through the python sqlite wrapper (that is part of the stdlib) and if it doesn't then not. This is true for any sqlite...
2
2038
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 /usr/lib/python2.{4,5}/site-packages/sqlite/main.py lrwxrwxrwx 1 root root 63 2007-12-27 15:29 /usr/lib/python2.4/site-packages/sqlite/main.py -/usr/share/pycentral/python-sqlite/site-packages/sqlite/main.py lrwxrwxrwx 1 root root 63 2007-12-27 15:29...
20
3970
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 using a sun machine (I normally use linux machines) and place this in my lunix home account would I be able to use python and sqlite? Any thoughts? I know its a bit of a stretch ...
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.