Save mysqldb-connection as class atribute to make it usable for all decandant classes of MYSQL_RED
This commit is contained in:
14
mysql_red.py
14
mysql_red.py
@@ -6,6 +6,9 @@ except ImportError:
|
||||
|
||||
class MYSQL_RED:
|
||||
|
||||
#Save mysqldb-connection as class attribute to use only one in descendant classes
|
||||
connection = False
|
||||
|
||||
def __init__( self, db_hostname, db_username, db_password, db_name ):
|
||||
"""
|
||||
Opens a connection to MySQL-DB
|
||||
@@ -13,11 +16,14 @@ class MYSQL_RED:
|
||||
@returns mysql-stream MySQL Connection
|
||||
"""
|
||||
|
||||
self.__connection = mysqldb.connect( host=db_hostname, user=db_username, passwd=db_password, db=db_name )
|
||||
# Connect to mysqldb only once
|
||||
if( type( self ).connection == False ):
|
||||
|
||||
def close( self ):
|
||||
type( self ).connection = mysqldb.connect( host=db_hostname, user=db_username, passwd=db_password, db=db_name )
|
||||
|
||||
def __del__( self ):
|
||||
"""
|
||||
Before deleting instance, close connection to MySQL-DB
|
||||
Before deleting class, close connection to MySQL-DB
|
||||
"""
|
||||
|
||||
self.__connection.close()
|
||||
type( self ).connection.close()
|
||||
|
||||
Reference in New Issue
Block a user