databaseconnection

This commit is contained in:
2024-11-20 15:46:01 +01:00
parent 0e8e03dc5d
commit 3d5035ffb3
8 changed files with 215 additions and 173 deletions

41
lib/DB/BusinessModel.py Normal file
View File

@@ -0,0 +1,41 @@
# This Python file uses the following encoding: utf-8
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, Slot
class BusinessModel(QAbstractTableModel):
def __init__(self, con):
super().__init__()
self.con = con
self.__data = self.__getData()
def __getData(self):
cursor = self.con.cursor()
cursor.execute("SELECT * FROM test")
return cursor.fetchall()
def rowCount(self, parent= QModelIndex()):
return len (self.__data)
def columnCount(self, parent= QModelIndex()):
return 3
def data(self, index, role= Qt.DisplayRole):
if role == Qt.DisplayRole:
row, col = index.row(), index.column()
return self.__data[row][col]
return None
def headerData(self, section, orientation, role= Qt.DisplayRole):
header= ["ID", "Kundenname", "Ort"]
if orientation == Qt.Horizontal and role ==Qt.DisplayRole:
return header[section]
return super().headerData(section, orientation, role)
#return None
@Slot(int)
def onRowClicked(self, row):
print(row)

22
lib/DB/DbManager.py Normal file
View File

@@ -0,0 +1,22 @@
# This Python file uses the following encoding: utf-8
import toml
import mysql.connector
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 __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