DbManager mariadb connection
This commit is contained in:
@@ -1,28 +1,26 @@
|
|||||||
# This Python file uses the following encoding: utf-8
|
# This Python file uses the following encoding: utf-8
|
||||||
import toml
|
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
|
||||||
|
|
||||||
class DbManager(object):
|
class DbManager(object):
|
||||||
__connection = None
|
__connection = None
|
||||||
__con_param = None
|
__con_param = None
|
||||||
def __new__ (cls):
|
__dbmanager = None
|
||||||
|
|
||||||
if not hasattr(cls, "__instance"):
|
def __new__(cls, dbconf = None):
|
||||||
cls.__instance = super().__new__(cls)
|
if cls.__dbmanager is None:
|
||||||
return cls.__instance
|
cls.__dbmanager = super(DbManager, cls).__new__(cls)
|
||||||
|
cls.__dbmanager.__initializeConfig(dbconf)
|
||||||
|
|
||||||
#def __init__ (self, dbconf, *args):
|
return cls.__dbmanager
|
||||||
@classmethod
|
|
||||||
def connectDB(cls, dbconf):
|
|
||||||
|
|
||||||
con_param = cls.__initializeConfig(dbconf)
|
def getConnection(cls):
|
||||||
cls.__connection = mysql.connector.connect(**con_param)
|
|
||||||
return cls.__connection
|
return cls.__connection
|
||||||
|
|
||||||
def __initializeConfig(dbconf):
|
def __initializeConfig(cls, dbconf):
|
||||||
|
cls.__con_param = { 'user': dbconf['DB_USER'], 'password': dbconf['DB_PASS'],
|
||||||
|
'port': int (dbconf['DB_PORT']), 'host': dbconf['DB_HOST'],
|
||||||
|
'database': dbconf['DB_NAME']
|
||||||
|
}
|
||||||
|
cls.__connection = mysql.connector.connect(**cls.__con_param)
|
||||||
|
|
||||||
con_param = {'user': dbconf['DB_USER'], 'password': dbconf['DB_PASS'], 'port': int (dbconf['DB_PORT']), 'host': dbconf['DB_HOST'], 'database': dbconf['DB_NAME']}
|
|
||||||
|
|
||||||
return con_param
|
|
||||||
|
|
||||||
|
|||||||
6
main.py
6
main.py
@@ -48,9 +48,11 @@ if __name__ == "__main__":
|
|||||||
bad_config = True
|
bad_config = True
|
||||||
bm = False
|
bm = False
|
||||||
else:
|
else:
|
||||||
con = DbManager().connectDB(config.getConfig()['database'])
|
dbconf = config.getConfig()['database']
|
||||||
bm = BusinessModel(con)
|
DbManager(dbconf)
|
||||||
|
bm = BusinessModel(DbManager().getConnection())
|
||||||
|
|
||||||
|
#print(con is con2)
|
||||||
|
|
||||||
engine.rootContext().setContextProperty("bm", bm)
|
engine.rootContext().setContextProperty("bm", bm)
|
||||||
engine.rootContext().setContextProperty("bad_config", bad_config) # print(f"Fehler: {i}")
|
engine.rootContext().setContextProperty("bad_config", bad_config) # print(f"Fehler: {i}")
|
||||||
|
|||||||
Reference in New Issue
Block a user