database connection

This commit is contained in:
2024-11-20 16:36:01 +01:00
parent 3d5035ffb3
commit a9804cbde1
5 changed files with 22 additions and 14 deletions

View File

@@ -72,7 +72,7 @@ Item {
Layout.fillWidth: true Layout.fillWidth: true
columnSpacing: 1 columnSpacing: 1
rowSpacing: 2 rowSpacing: 2
model: dbm model: bm
selectionBehavior: TableView.SelectRows selectionBehavior: TableView.SelectRows
selectionModel: ItemSelectionModel selectionModel: ItemSelectionModel
@@ -109,7 +109,7 @@ Item {
onClicked: onClicked:
{ {
dbm.onRowClicked(row) bm.onRowClicked(row)
testTable.selectionModel.select(testTable.model.index(row, 0), ItemSelectionModel.SelectCurrent | ItemSelectionModel.Rows) testTable.selectionModel.select(testTable.model.index(row, 0), ItemSelectionModel.SelectCurrent | ItemSelectionModel.Rows)

View File

@@ -52,7 +52,7 @@ class ConfigLoader(QObject):
def getConfig(self): def getConfig(self):
print(self.__config['database'])
return self.__config return self.__config
def createConfig(self): def createConfig(self):

View File

@@ -13,7 +13,7 @@ class BusinessModel(QAbstractTableModel):
def __getData(self): def __getData(self):
cursor = self.con.cursor() cursor = self.con.cursor()
cursor.execute("SELECT * FROM test") cursor.execute("SELECT * FROM users")
return cursor.fetchall() return cursor.fetchall()
def rowCount(self, parent= QModelIndex()): def rowCount(self, parent= QModelIndex()):

View File

@@ -7,16 +7,22 @@ class DbManager(object):
__connection = None __connection = None
__con_param = None __con_param = None
def __new__ (cls): def __new__ (cls):
if not hasattr(cls, "__instance"): if not hasattr(cls, "__instance"):
cls.__instance = super().__new__(cls) cls.__instance = super().__new__(cls)
return cls.__instance return cls.__instance
def __init__ (self, dbconf): #def __init__ (self, dbconf, *args):
__con_param = __initializeConfig(dbconf) @classmethod
self.__connection = mysql.connector.connect(__con_param) def connectDB(cls, dbconf):
return self.__connection
def __initializeConfig(self, dbconf): con_param = cls.__initializeConfig(dbconf)
__con_param = {'user': dbconf['DB_USER'], 'password': dbconf['DB_PASS'], 'port': dbconf['DB_PORT'], 'host': dbconf['DB_HOST'], 'name': dbconf['DB_NAME']} cls.__connection = mysql.connector.connect(**con_param)
return __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

View File

@@ -9,7 +9,7 @@ from lib.DB.BusinessModel import BusinessModel
import rc_pyqcrm import rc_pyqcrm
import rc_qml import rc_qml
import sqlite3 import sqlite3
import lib.DB.DbManager from lib.DB.DbManager import DbManager
# [pyqcrm] # [pyqcrm]
# program-name="" # program-name=""
@@ -43,11 +43,13 @@ if __name__ == "__main__":
config = ConfigLoader() config = ConfigLoader()
con = DbManager()
bm = BusinessModel(con)
if not config.getConfig(): if not config.getConfig():
bad_config = True bad_config = True
bm = False
else:
con = DbManager().connectDB(config.getConfig()['database'])
bm = BusinessModel(con)
engine.rootContext().setContextProperty("bm", bm) engine.rootContext().setContextProperty("bm", bm)