mysql.connector.errors.OperationalError: MySQL Connection not available.
出现上面的错的原因是因为,我用游标拿完数据之后,再函数返回值之前,将游标关闭,出现了如上的错误。
错误写法:
def getArticle(self, _from, _to, _ws): db = computerDb.GetMysql() sql_a = "select title,author,publish_time,content,news_id,lang,version,oringin_url,full_content from military_news where website='{}' and time >='{}' and time <='{}' order by publish_time desc".format(_ws, _from, _to) print(sql_a) # c = self.db.cursor() c = db.cursor() c.execute(sql_a) rst = c.fetchall() return rst
正确写法:
def getArticle(self, _from, _to, _ws): db = computerDb.GetMysql() sql_a = "select title,author,publish_time,content,news_id,lang,version,oringin_url,full_content from military_news where website='{}' and time >='{}' and time <='{}' order by publish_time desc".format(_ws, _from, _to) print(sql_a) # c = self.db.cursor() c = db.cursor() c.execute(sql_a) rst = c.fetchall() rsst = rst #这一句比较重要,报错的原因就是因为数据再未使用之前,就将游标关闭,导致错误的发生 c.close() return rsst
记录上述错误的原因是在这里已经犯过类似的错误,警醒自己下次注意,也是为了下次犯错误,能够一眼知道在哪里出错了。