30 lines
690 B
Python
30 lines
690 B
Python
|
|
try:
|
|
import oursql as mysqldb
|
|
except ImportError:
|
|
import MySQLdb as mysqldb
|
|
|
|
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
|
|
|
|
@returns mysql-stream MySQL Connection
|
|
"""
|
|
|
|
# Connect to mysqldb only once
|
|
if( type( self ).connection == False ):
|
|
|
|
type( self ).connection = mysqldb.connect( host=db_hostname, user=db_username, passwd=db_password, db=db_name )
|
|
|
|
def __del__( self ):
|
|
"""
|
|
Before deleting class, close connection to MySQL-DB
|
|
"""
|
|
|
|
type( self ).connection.close()
|