[Tutor] [Fwd: Please help, I'm a newbie]

Dick Kniep d.j.kniep@chello.nl
23 Sep 2002 16:02:36 +0200


Hi there,

I am trying to write a piece of coding that uses a standard routine to
open SQL tables into dictionaries.I do not wish to enter all the
columns, because that severely cuts the flexibility of my code.
Therefore, I want to get my columns from the database. Here are a few
snippets of my code. 

The first part gives the routine (TableObjs) uses a config file to
determine whether a table should be processed or not. It it is to be
processed, it uses another list of tuples to determine the actual
tablename as it is defined in Postgresql. Next a statement is defined,
and that statement is "exec"ed.

(It's written on a Windows system, so please forgive the CRLF's) ;-)

....
....

def TableObjs(dbObj, config):

    """ Get Tables in Dictionaries """

    db = SQLDict(dbObj)
    for x in Tables:
        h = string.lower('memtable.' + x)
        if config[h] == '1':
            if Tables[x][0] == "'":
                Tabnm = string.replace(Tables[x],"'","")
            elif Tables[x][0] == '"':
                Tabnm = string.replace(Tables[x],'"','')
            else:
                Tabnm = Tables[x]
                
            stmt = "db." + Tabnm + " = db.Table(" + Tables[x] + ")"
            exec stmt

    return db
....
....

The next part is a standard routine that I downloaded. It works properly
if I use it in the documented way. However, in the _init_ routine I have
added a few statements, and now it can't find the 

class SQLDict:

    """SQLDict: An object class which implements something resembling
    a Python dictionary on top of an SQL DB-API database."""

    def __init__(self, db):

	"""Create a new SQLDict object.
	db: an SQL DB-API database connection object"""

	# Most database objects are native C, so they can't be subclassed.
	self.db = db

    def __del__(self):	self.close()

    def close(self):
	try: self.db.close()
	except: pass

    def __getattr__(self, attr):
	# Get any other interesting attributes from the base class.
	return getattr(self.db, attr)

    class _Table:

	"""Table handler for a SQLDict object. These should not be created
	directly by user code."""

	def __init__(self, db, table, columns=[], updatecolumns=[]):

	    """Construct a new table definition. Don't invoke this
	    directly. Use Table method of SQLDict instead."""

	    self.db = db
	    self.table = table
	    self.columns = columns
        if not columns:
            Sqlstmt = "SELECT * FROM " + self.table
            cursor = db.cursor(Sqlstmt)
            columns = [ d[0] for d in cursor.description ]
	    self.SELECT = "SELECT %s FROM %s " % \
			  (self._columns(columns),
			   self.table)
	    self.INSERT = "INSERT INTO %s (%s)\n    VALUES (%s) " % \
			  (self.table,
			   self._columns(columns),
			   self._values(columns))
	    self.DELETE = "DELETE FROM %s " % self.table
	    self.Update(updatecolumns or columns)

    def Table(self, table, columns=[], updatecolumns=[]):

	"""Add a new Table member.

	Usage: db.Table(tablename, columns)
	Where: tablename  = name of table in database
               columns    = tuple containing names of columns of
interest

	       """

	return self._Table(self.db, table, columns, updatecolumns)

The error I get is that the name "columns" is not defined. But that is
weird because it gets the error on the "if not columns", and I would
expect the error on the statement before that, because there columns is
referenced. Furthermore, I want to test whether the length of the tuple
is 0, if it is, I want to generate the list of columns automatically, by
defining a cursor, and reading the description. I have included the 2
routines, so if you could help me PLEASE!

Having searched a little further, it looks a little less weird, but I still 
do not understand what is happening. It seems that the error occurs on the import 
of the routine. But I still don'y know what is happening here....

Kind regards,
D Kniep