添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

#->ReferenceError: 弱引用的对象不再存在

0 人关注

This is the code produce error:

import mysql.connector
import datetime
class Command:
    def __init__(self):
        mydb = mysql.connector.connect(host='localhost', passwd='1234', user='root', database='customers')
        self.mycursor = mydb.cursor()
    def execute(self, contest_id, url, Questions):
        date = datetime.date.today()
        Time = datetime.datetime.now().strftime("%I:%M")
        self.mycursor.execute(f"INSERT INTO contest(contest_name, url_tag, Questions, At_date, At_time) VALUES('{contest_id}', '{url}', {Questions}, '{date}', '{Time}')")

我正在运行一个Python代码,我得到以下错误信息。

    Traceback (most recent call last):
   File "C:\python39\lib\site-packages\mysql\connector\cursor.py", line 518, in execute
    if not self._connection:
ReferenceError: weakly-referenced object no longer exists
    
python
mysql
weak-references
Suman Kumar
Suman Kumar
发布于 2022-05-08
1 个回答
High-Octane
High-Octane
发布于 2022-05-08
已采纳
0 人赞同

我的猜测是,你的连接丢失了,因为它不属于你的类。你能用这种方式试试吗?

import mysql.connector
import datetime
class Command:
    def __init__(self):
        self.conn = mysql.connector.connect(host='localhost', passwd='1234', user='root', database='customers')
        self.mycursor = self.conn.cursor()
    def execute(self, contest_id, url, Questions):
        date = datetime.date.today()