database_mysql.py 文件源码

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

项目:Penny-Dreadful-Tools 作者: PennyDreadfulMTG 项目源码 文件源码
def __init__(self, db):
        warnings.filterwarnings('error', category=MySQLdb.Warning)
        try:
            self.name = db
            host = configuration.get('mysql_host')
            port = configuration.get('mysql_port')
            if str(port).startswith('0.0.0.0:'):
                # Thanks Docker :/
                port = int(port[8:])
            user = configuration.get('mysql_user')
            passwd = configuration.get('mysql_passwd')
            self.connection = MySQLdb.connect(host=host, port=port, user=user, passwd=passwd, use_unicode=True, charset='utf8', autocommit=True)
            self.cursor = self.connection.cursor(MySQLdb.cursors.DictCursor)
            self.execute('SET NAMES utf8mb4')
            try:
                self.execute("USE {db}".format(db=db))
            except DatabaseException:
                print('Creating database {db}'.format(db=db))
                self.execute('CREATE DATABASE {db} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci'.format(db=db))
                self.execute('USE {db}'.format(db=db))
        except MySQLdb.Error as e:
            raise DatabaseException('Failed to initialize database in `{location}`'.format(location=db)) from e
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号