mysql.connector.errors.OperationalError: MySQL Connection not available.

时间:2021-09-01
本文章向大家介绍mysql.connector.errors.OperationalError: MySQL Connection not available.,主要包括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

记录上述错误的原因是在这里已经犯过类似的错误,警醒自己下次注意,也是为了下次犯错误,能够一眼知道在哪里出错了。

原文地址:https://www.cnblogs.com/lxz123/p/15215080.html