From a9804cbde13a011e15ce5b9ba176230555f10b47a6d096ba3a063e46738953a3 Mon Sep 17 00:00:00 2001 From: Marco Gatzen Date: Wed, 20 Nov 2024 16:36:01 +0100 Subject: [PATCH] database connection --- Gui/EmployeTables.qml | 4 ++-- lib/ConfigLoader.py | 2 +- lib/DB/BusinessModel.py | 2 +- lib/DB/DbManager.py | 20 +++++++++++++------- main.py | 8 +++++--- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Gui/EmployeTables.qml b/Gui/EmployeTables.qml index fd955b2..fd1017b 100644 --- a/Gui/EmployeTables.qml +++ b/Gui/EmployeTables.qml @@ -72,7 +72,7 @@ Item { Layout.fillWidth: true columnSpacing: 1 rowSpacing: 2 - model: dbm + model: bm selectionBehavior: TableView.SelectRows selectionModel: ItemSelectionModel @@ -109,7 +109,7 @@ Item { onClicked: { - dbm.onRowClicked(row) + bm.onRowClicked(row) testTable.selectionModel.select(testTable.model.index(row, 0), ItemSelectionModel.SelectCurrent | ItemSelectionModel.Rows) diff --git a/lib/ConfigLoader.py b/lib/ConfigLoader.py index 7b25cf1..c659117 100644 --- a/lib/ConfigLoader.py +++ b/lib/ConfigLoader.py @@ -52,7 +52,7 @@ class ConfigLoader(QObject): def getConfig(self): - print(self.__config['database']) + return self.__config def createConfig(self): diff --git a/lib/DB/BusinessModel.py b/lib/DB/BusinessModel.py index eb4e7bc..f5644c8 100644 --- a/lib/DB/BusinessModel.py +++ b/lib/DB/BusinessModel.py @@ -13,7 +13,7 @@ class BusinessModel(QAbstractTableModel): def __getData(self): cursor = self.con.cursor() - cursor.execute("SELECT * FROM test") + cursor.execute("SELECT * FROM users") return cursor.fetchall() def rowCount(self, parent= QModelIndex()): diff --git a/lib/DB/DbManager.py b/lib/DB/DbManager.py index f01a2cc..c01538a 100644 --- a/lib/DB/DbManager.py +++ b/lib/DB/DbManager.py @@ -7,16 +7,22 @@ class DbManager(object): __connection = None __con_param = None def __new__ (cls): + if not hasattr(cls, "__instance"): cls.__instance = super().__new__(cls) return cls.__instance - def __init__ (self, dbconf): - __con_param = __initializeConfig(dbconf) - self.__connection = mysql.connector.connect(__con_param) - return self.__connection + #def __init__ (self, dbconf, *args): + @classmethod + def connectDB(cls, dbconf): - def __initializeConfig(self, dbconf): - __con_param = {'user': dbconf['DB_USER'], 'password': dbconf['DB_PASS'], 'port': dbconf['DB_PORT'], 'host': dbconf['DB_HOST'], 'name': dbconf['DB_NAME']} - return __con_param + con_param = cls.__initializeConfig(dbconf) + cls.__connection = mysql.connector.connect(**con_param) + return cls.__connection + + def __initializeConfig(dbconf): + + 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 diff --git a/main.py b/main.py index 4423c10..193a5a7 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ from lib.DB.BusinessModel import BusinessModel import rc_pyqcrm import rc_qml import sqlite3 -import lib.DB.DbManager +from lib.DB.DbManager import DbManager # [pyqcrm] # program-name="" @@ -43,11 +43,13 @@ if __name__ == "__main__": config = ConfigLoader() - con = DbManager() - bm = BusinessModel(con) if not config.getConfig(): bad_config = True + bm = False + else: + con = DbManager().connectDB(config.getConfig()['database']) + bm = BusinessModel(con) engine.rootContext().setContextProperty("bm", bm)