pysqlite smart search

Bret Fledderjohn freelancer317 at gmail.com
Mon Feb 23 10:02:51 EST 2009


2009/2/23 klia <alwaseem307ster at yahoo.com>

>
>
>
> klia wrote:
> >
> >  Hey guys;
> >
> > I am trying to develop a tiny program using python to search inside
> sqlite
> > database with file extension is .db in which the program will ask users
> to
> > enter their search query and base on that it will retrieve the results
> But
> >
> > I need the program to have some smartness in search mechanism in which
> the
> > program will guess that the user is looking for these key words in the
> > database.
> >
> > so far i came up with this but the search ain't smart, i have to write
> the
> > full key word in order to retrieve specific information.
> >
> > from pysqlite2 import dbapi2 as sqlite3
> >
> > connection = sqlite3.connect('Photos.db')
> > memoryConnection = sqlite3.connect(':memory:')
> > cursor = connection.cursor()
> > prompt = 'Enter your search query?\n'
> > keyword = raw_input(prompt)
> > if cursor.execute('SELECT * FROM photos WHERE Tag LIKE ?',[keyword]):
> > print cursor.fetchall()
> > else:
> > cursor.execute('SELECT * FROM photos WHERE Date LIKE ?', [keyword])
> > print cursor.fetchall()
> >
> > Any ideas and
> > thanks in advance
> > Reply
> >
>
> thank you man, it worked perfectly but one more thing;
>
> when i print rows it retrieves the whole rows of info which i don't need, i
> need just to retrieve the name of the photo.
> so how am i gonna do that?


It depends on the structure of your table, but you're going to have to
change the following lines:
>if cursor.execute('SELECT * FROM photos WHERE Tag LIKE ?',[keyword]):
>cursor.execute('SELECT * FROM photos WHERE Date LIKE ?', [keyword])
* indicates all fields in the selected records, so you need to
change the * to the field that contains the name of the photo.  For example:
if cursor.execute('SELECT name FROM photos WHERE Tag LIKE ?',[keyword]):

--
> View this message in context:
> http://www.nabble.com/pysqlite-smart-search-tp22157116p22161119.html
> Sent from the Python - python-list mailing list archive at Nabble.com.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
- Bret
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090223/2b1b9625/attachment-0001.html>


More information about the Python-list mailing list