Rumah > Soal Jawab > teks badan
我感觉问题出在数据库的连接部分。因为以前一直报错#2006 说mysql connection has gone away
后来我修改了下,连接的时候进行下ping,出现exception的时候新建一个连接
数据库连接部分代码:
class Database: configs = { # default configs "host": "localhost", "port": 3306, # int "db": "", "user": "", "passwd": "", "charset": "utf8" } conn = None debug = True query_times = 0 SQL = None @classmethod def config(cls, debug=True, **configs): cls.configs.update(configs) cls.debug = debug @classmethod def new_conn(cls): cls.conn = MySQLdb.connect( cursorclass=MySQLdb.cursors.DictCursor, **cls.configs ) @classmethod def connect(cls): # singleton if not cls.conn or not cls.conn.open: cls.new_conn() try: cls.conn.ping() # ping to test if the connection is working except MySQLdb.OperationalError: cls.new_conn() return cls.conn