databaseconnection
This commit is contained in:
41
lib/DB/BusinessModel.py
Normal file
41
lib/DB/BusinessModel.py
Normal 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
22
lib/DB/DbManager.py
Normal 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
|
||||
|
||||
Reference in New Issue
Block a user