Hi to all,
I'm using adodb for accessing mysql and postgres. My problem relies on
the mysql access.
Sometimes, when I try to execute a query (using ExecTrans method
below), I get this error:
'NoneType' object has no attribute 'cursor'
Maybe this error ocurrs not in my code, but in the mysql module. Does
anyone had had this problem?
Thanks.
The following is my DB connection class.
class DB:
def __init__(self, host=None, user=None, password=None,
database=None):
self.host =
[host,variables. IP_DB][isinstance(host ,(types.NoneTyp e))]
self.user =
[user,variables. USER][isinstance(user ,(types.NoneTyp e))]
self.password =
[password,variab les.PASSWORD][isinstance(pass word,(types.Non eType))]
self.database =
[database,variab les.DATABASE][isinstance(data base,(types.Non eType))]
self.bActive = True
self.bConnected = False
def OpenDB(self):
success = False
if self.bActive:
try:
self.conn = adodb.NewADOCon nection('mysql' )
if not self.conn:
Log.log("Can't connecto to DB-Mysql")
success = False
else :
#Create the connection
ret = self.conn.Conne ct(self.host, self.user,
self.password, self.database)
if ret:
Log.log("DB-Mysql connection stablished")
success = True
except:
success = False
pass
else:
print "DB connection disabled"
self.bConnected = success
return success
def Exec(self,query ,simple=False):
resultado = list()
#Open a DB connection
if self.OpenDB():
Log.log("Execut ing Query: " + str(query))
cursor = self.conn.Execu te(query)
if cursor:
while not cursor.EOF:
if not simple:
resultado.appen d(cursor.GetRow Assoc())
cursor.MoveNext ()
else :
resultado.appen d(cursor.FetchR ow())
#Close Cursor
cursor.Close()
else:
resultado = None
self.CloseDB()
else:
resultado = None
return resultado
def ExecTrans(self, lQueries):
success = False
if self.OpenDB():
print "Executing Querys: " + str(lQueries)
#Begin Transaction
self.conn.Begin Trans()
#Execute each query in lQueries
for query in lQueries:
self.conn.Execu te(query)
if self.conn._errn o != 0 :
success = False
break;
else:
success = True
#End of transaction
if success:
self.conn.Commi tTrans()
else:
self.conn.Rollb ackTrans()
#Closing DB
self.CloseDB()
return success
def TestConnection( self):
if self.OpenDB():
self.CloseDB()
return True
return False
def CloseDB(self):
if self.conn.IsCon nected() :
self.conn.Close ()
return
I'm using adodb for accessing mysql and postgres. My problem relies on
the mysql access.
Sometimes, when I try to execute a query (using ExecTrans method
below), I get this error:
'NoneType' object has no attribute 'cursor'
Maybe this error ocurrs not in my code, but in the mysql module. Does
anyone had had this problem?
Thanks.
The following is my DB connection class.
class DB:
def __init__(self, host=None, user=None, password=None,
database=None):
self.host =
[host,variables. IP_DB][isinstance(host ,(types.NoneTyp e))]
self.user =
[user,variables. USER][isinstance(user ,(types.NoneTyp e))]
self.password =
[password,variab les.PASSWORD][isinstance(pass word,(types.Non eType))]
self.database =
[database,variab les.DATABASE][isinstance(data base,(types.Non eType))]
self.bActive = True
self.bConnected = False
def OpenDB(self):
success = False
if self.bActive:
try:
self.conn = adodb.NewADOCon nection('mysql' )
if not self.conn:
Log.log("Can't connecto to DB-Mysql")
success = False
else :
#Create the connection
ret = self.conn.Conne ct(self.host, self.user,
self.password, self.database)
if ret:
Log.log("DB-Mysql connection stablished")
success = True
except:
success = False
pass
else:
print "DB connection disabled"
self.bConnected = success
return success
def Exec(self,query ,simple=False):
resultado = list()
#Open a DB connection
if self.OpenDB():
Log.log("Execut ing Query: " + str(query))
cursor = self.conn.Execu te(query)
if cursor:
while not cursor.EOF:
if not simple:
resultado.appen d(cursor.GetRow Assoc())
cursor.MoveNext ()
else :
resultado.appen d(cursor.FetchR ow())
#Close Cursor
cursor.Close()
else:
resultado = None
self.CloseDB()
else:
resultado = None
return resultado
def ExecTrans(self, lQueries):
success = False
if self.OpenDB():
print "Executing Querys: " + str(lQueries)
#Begin Transaction
self.conn.Begin Trans()
#Execute each query in lQueries
for query in lQueries:
self.conn.Execu te(query)
if self.conn._errn o != 0 :
success = False
break;
else:
success = True
#End of transaction
if success:
self.conn.Commi tTrans()
else:
self.conn.Rollb ackTrans()
#Closing DB
self.CloseDB()
return success
def TestConnection( self):
if self.OpenDB():
self.CloseDB()
return True
return False
def CloseDB(self):
if self.conn.IsCon nected() :
self.conn.Close ()
return