__init__.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:BigBrotherBot-For-UrT43 作者: ptitbigorneau 项目源码 文件源码
def __init_storage(self):
        """
        Initialize the connection with the SQLite database.
        :raise DatabaseError: if the database schema is not consistent.
        """
        is_new_database = not os.path.isfile(B3_STORAGE)
        self.storage = sqlite3.connect(B3_STORAGE, check_same_thread=False)
        self.storage.isolation_level = None  # set autocommit mode
        self.storage.row_factory = sqlite3.Row  # allow row index by name
        if is_new_database:
            # create new schema
            self.__build_schema()
        else:
            # check database schema
            LOG.debug('checking database schema')
            cursor = self.storage.cursor()
            cursor.execute("""SELECT * FROM sqlite_master WHERE type='table'""")
            tables = [row[1] for row in cursor.fetchall()]
            cursor.close()

            if 'b3' not in tables:
                LOG.debug('database schema is corrupted: asking the user if he wants to rebuild it')

                msgbox = QMessageBox()
                msgbox.setIcon(QMessageBox.Critical)
                msgbox.setText('The database schema is corrupted and must be rebuilt. Do you want to proceed?')
                msgbox.setInformativeText('NOTE: all the previously saved data will be lost!')
                msgbox.setStandardButtons(QMessageBox.No | QMessageBox.Yes)
                msgbox.setDefaultButton(QMessageBox.No)
                msgbox.exec_()

                if msgbox.result() == QMessageBox.No:
                    # critical will raise an exception which will terminate the QApplication
                    LOG.critical('could not start B3: database schema is corrupted!')

                try:
                    os.remove(B3_STORAGE)
                    self.__build_schema()
                except Exception, err:
                    raise LOG.critical('could initialize SQLite database: %s (%s): make sure B3 has permissions to '
                                       'operate on this file, or remove it manually', B3_STORAGE, err)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号