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安装扩展的几种方法
    navicat连接linux系统中mysql-错误:10038
    linux下报错bash: service: command not found
    linux配置防火墙和重启防火墙
    linux 环境安装
    匿名函数
    workman的学习总结
    xampp/apache启动失败解决方法
    Linux 查看IP
    慢查询日志
  • 原文地址:https://www.cnblogs.com/lxz123/p/15215080.html
Copyright © 2011-2022 走看看