[Tutor] module with static global
János Juhász
janos.juhasz at VELUX.com
Tue Jul 12 15:56:02 CEST 2005
Dear Guys!
I am using a class for connecting to an sql database via odbc.
It is like this:
import dbi, odbc
class scalaDB:
def __init__(self):
self.cn =
odbc.odbc('DSN=scalaDB;UID=query;PWD=query;DATABASE=scalaDB')
def closeDB(self):
self.cn.close()
def Execute(self, sql):
cr = self.cn.cursor()
cr.execute(sql)
cr.close()
def Query(self, sql):
try:
cr = self.cn.cursor()
cr.execute(sql)
self.colnames = [field_prop[0] for field_prop in
cr.description]
self.result = cr.fetchall()
self.rownum = len(self.result)
return self.result
except:
self.colnames = [None]
self.result = [[None]]
self.rownum = 0
return [[None]]
def QueryValue(self, sql):
try:
cr = self.cn.cursor()
cr.execute(sql)
self.colnames = [field_prop[0] for field_prop in
cr.description]
self.result = cr.fetchall()
self.rownum = len(self.result)
return self.result[0][0]
except:
self.colnames = [None]
self.result = None
self.rownum = 0
return None
I also have some other classes represented in this database as records. As
the records are in the database, they has to have a scalaDB instance for
running any sql statement on it. Currently I send a scalaDB instance to
every record-based class in the __init__(): proc.
class Component:
def __init__(self, scalaDB, StockCode, Qty=1, Parent=None):
self.scalaDB = scalaDB
self.StockCode = StockCode
self.Parent = Parent
self.Qty = Qty ## Qty in
the BOM
self.Components = self.GetComponents()
def GetParent(self):
return self.Parent
def __getattr__(self, name):
if 'SC' in name:k
value = self.scalaDB.QueryValue("select %s from SC010100
where SC01001 = '%s'" % (name, self.StockCode) )
return value
How have I modify this to use my scalaDB class in this way:
>>>import scalaDB
>>>xy = scalaDB.Query(sql)
Best regards,
Janos Juhasz
More information about the Tutor
mailing list