database connection
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ class ConfigLoader(QObject):
|
||||
|
||||
|
||||
def getConfig(self):
|
||||
print(self.__config['database'])
|
||||
|
||||
return self.__config
|
||||
|
||||
def createConfig(self):
|
||||
|
||||
@@ -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()):
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
8
main.py
8
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)
|
||||
|
||||
Reference in New Issue
Block a user