def refresh(self): """ Public slot to refresh the connection tree. """ self.__connectionTree.clear() self.cleared.emit() connectionNames = QSqlDatabase.connectionNames() foundActiveDb = False for name in connectionNames: root = QTreeWidgetItem(self.__connectionTree) db = QSqlDatabase.database(name, False) root.setText(0, self.__dbCaption(db)) if name == self.__activeDb: foundActiveDb = True self.__setActive(root) if db.isOpen(): tables = db.tables() for table in tables: itm = QTreeWidgetItem(root) itm.setText(0, table) if not foundActiveDb and connectionNames: self.__activeDb = connectionNames[0] self.__setActive(self.__connectionTree.topLevelItem(0))
def refresh(self): """ Public slot to refresh the connection tree. """ self.__connectionTree.clear() self.cleared.emit() connectionNames = QSqlDatabase.connectionNames() foundActiveDb = False for name in connectionNames: root = QTreeWidgetItem(self.__connectionTree) db = QSqlDatabase.database(name, False) root.setText(0, self.__dbCaption(db)) if name == self.__activeDb: foundActiveDb = True self.__setActive(root) if db.isOpen(): tables = db.tables() for table in tables: itm = QTreeWidgetItem(root) itm.setText(0, table) if not foundActiveDb and connectionNames: self.__activeDb = connectionNames[0] self.__setActive(self.__connectionTree.topLevelItem(0))
def __uiStartUp(self): """ Private slot to do some actions after the UI has started and the main loop is up. """ for warning in self.__warnings: E5MessageBox.warning(self, self.tr("SQL Browser startup problem"), warning) if len(QSqlDatabase.connectionNames()) == 0: self.__browser.addConnectionByDialog()
def clean_database(): db = boot.connect_to_db() try: query = QSqlQuery() query.exec_("DELETE FROM tracks") query.exec_("DELETE FROM power_hours") query.exec_("DELETE FROM migrations") finally: db.close() for name in QSqlDatabase.connectionNames(): QSqlDatabase.removeDatabase(name)
def __uiStartUp(self): """ Private slot to do some actions after the UI has started and the main loop is up. """ for warning in self.__warnings: E5MessageBox.warning( self, self.tr("SQL Browser startup problem"), warning) if len(QSqlDatabase.connectionNames()) == 0: self.__browser.addConnectionByDialog()
def __setActive(self, itm): """ Private slot to set an item to active. @param itm reference to the item to set as the active item (QTreeWidgetItem) """ for index in range(self.__connectionTree.topLevelItemCount()): if self.__connectionTree.topLevelItem(index).font(0).bold(): self.__setBold(self.__connectionTree.topLevelItem(index), False) if itm is None: return self.__setBold(itm, True) self.__activeDb = QSqlDatabase.connectionNames()[ self.__connectionTree.indexOfTopLevelItem(itm)]
def __setActive(self, itm): """ Private slot to set an item to active. @param itm reference to the item to set as the active item (QTreeWidgetItem) """ for index in range(self.__connectionTree.topLevelItemCount()): if self.__connectionTree.topLevelItem(index).font(0).bold(): self.__setBold( self.__connectionTree.topLevelItem(index), False) if itm is None: return self.__setBold(itm, True) self.__activeDb = QSqlDatabase.connectionNames()[ self.__connectionTree.indexOfTopLevelItem(itm)]
while querySelect.previous(): print(querySelect.value(name), querySelect.value(email)) # print(querySelect.value(name)) # print(querySelect.value(email)) # Create the application's window ''' https://realpython.com/python-pyqt-database/#closing-and-removing-database-connections ''' if con.isOpen(): con.close() # remove database connection # before remove, everything that use connection is deleted # or set to use a different data source print(QSqlDatabase.connectionNames()) QSqlDatabase.removeDatabase(QSqlDatabase.database().connectionName()) print(QSqlDatabase.connectionNames()) win = QLabel("Connection Successfully Opened!") win.setWindowTitle("App Name") win.resize(200, 100) win.show() sys.exit(app.exec_())