gpt4 book ai didi

python - ReferenceError : weakly-referenced object no longer exists, Python3

转载 作者:行者123 更新时间:2023-11-30 22:36:21 28 4
gpt4 key购买 nike

我正在 Python 3 中尝试这个简单的代码:

import mysql.connector
class App():
def __init__(self):
self.conexion = mysql.connector.connect(user='?',
password='?',
host='?',
db='?')
self.cursor = self.conexion.cursor()
def __del__(self):
self.conexion.close()
self.cursor.close() # The code works if i remove this line;

myApp = App()

如果我删除 self.cursor.close() 行,该代码可在 Python 2 和 Python 3 中运行。这应该发生吗?

问题是当我尝试在 __del__ 中使用 self.cursor.close() 时,如果 self.cursor.close() 代码有效__init__ 里面,否则,我什至可以创建表。

我遇到的错误是:

Exception ignored in: <bound method MySQL_App.__del__ of <__main__.MySQL_App object at 0x7f6f87f42278>>
Traceback (most recent call last):
File "filename.py", line 22, in __del__
File "/usr/lib/python3.4/site-packages/mysql/connector/cursor.py", line 338, in close
File "/usr/lib/python3.4/site-packages/mysql/connector/cursor.py", line 310, in _have_unread_result
ReferenceError: weakly-referenced object no longer exists

最佳答案

你做错了。由于在连接之后 创建游标,因此需要在连接 之前关闭它。

self.cursor.close()
self.conexion.close()

关于python - ReferenceError : weakly-referenced object no longer exists, Python3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32447128/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com