• 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

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

  • 相关阅读:
    Vim配置IDE开发环境
    Win7任务计划自由预设系统定时自动关机
    awk中文手册
    awk简明教程
    Linux之mount命令详解
    VirtualBox内Linux系统与Windows共享文件夹
    堆排序详解
    int main(int argc,char* argv[])参数详解
    GDB调试详解
    VirtualBox中虚拟Ubuntu添加新的虚拟硬盘
  • 原文地址:https://www.cnblogs.com/lxz123/p/15215080.html
Copyright © 2020-2023  润新知