DBSeek

godwin godwinburby at rediffmail.com
Tue Jul 12 01:32:16 EDT 2005


Hello there,
      I need some thoughts about a web application that i am dreaming
and
drooling about in python. I want a search page to look exactly like
Google. But when i press the search button, it should search a database
in an rdbms like Oracle and provide results.
             For example, if my keywords are "all customers with names
starting with 'God'" it should somehow search table CUSTOMER , with
following query :
	SELECT CUSTNAME FROM CUSTOMER WHERE CUSTNAME LIKE 'God%'
So we basically need is a good python parser module which parses the
keywords into an sql or sqls and list the results. I can look in the
keywords for table and column synonyms and map it into table and column
names and create sql but it's not a foolproof idea as we all know that
english is a very vague language. The above idea wil fail , if i can't
identify table,column names ,operators and values in their logical
orders so as to create a syntactically correct sql. If there are more
tables involved, i should also think of joining tables
(inner,outer,equi joins). All I want is some enlightening thoughts from
the python hackers(i mean programmers) out there.Plz polish your grey
cells and let me know your thoughts.

# this is my basic and foolish keywordparser
# the object db provides the table as well as column names
# as u can see it may or may not work even for a single table
class KeyWordParser(object):
    def __init__(self,keywords,db):
        self.keywords = keywords.upper().split()
        self.db = db
        self.tables = []
        self.columns = []

    def parse2sql(self):
        for word in self.keywords:
            if word in self.db.tables():
                self.tables.append(word)

        for word in self.keywords:
            for table in self.tables:
                for column in self.db.columns(table):
                    if column == word:
                        self.columns.append(column)
        sql = 'SELECT %s FROM %s' % (','.join(self.columns) or
'*',','.join(self.tables))
        return sql




More information about the Python-list mailing list