zoukankan      html  css  js  c++  java
  • 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

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

  • 相关阅读:
    php 循环
    php 函数
    bzoj4541 [Hnoi2016]矿区
    bzoj4836 [Lydsy2017年4月月赛]二元运算
    bzoj4555 [Tjoi2016&Heoi2016]求和
    COGS2287 [HZOI 2015]疯狂的机器人
    bzoj3142 [Hnoi2013]数列
    bzoj4318 OSU!
    bzoj4247 挂饰
    bzoj2756 [SCOI2012]奇怪的游戏
  • 原文地址:https://www.cnblogs.com/lxz123/p/15215080.html
Copyright © 2011-2022 走看看