python - weakly-referenced object no longer exists? 數(shù)據(jù)庫(kù)連接關(guān)閉問(wèn)題
問(wèn)題描述
#! python3import mysql.connectorclass A: def __init__(self):self.dbconfig = {....}self.conn = mysql.connector.connect(**self.dbconfig)self.cur = self.conn.cursor() def __del__(self):self.cur.close()self.conn.close()
數(shù)據(jù)庫(kù)連接也成功了,但是執(zhí)行完該類(lèi)后就會(huì)報(bào)錯(cuò):
Exception ignored in: <bound method A.__del__ of <__main__.**** object at 0x0000000001151358>>Traceback (most recent call last): File '****.py', line *, in __del__ File '*****Python35libsite-packagesmysqlconnectorcursor.py', line 344, in closeReferenceError: weakly-referenced object no longer exists
還請(qǐng)知道什么原因的司機(jī)解惑!萬(wàn)分感謝!按stackoverflow上的方案就是我寫(xiě)的這樣,但是還是有問(wèn)題,難道是我的翻譯軟件有問(wèn)題?
問(wèn)題解答
回答1:好吧,自己答一下。并不知道是什么原因?qū)е碌腻e(cuò)誤,等以后水平高點(diǎn)再來(lái)回答。暫時(shí)解決辦法:
import mysql.connectorclass A: def __init__(self):self.dbconfig = {...}try: self.conn = mysql.connector.connect(**self.dbconfig) self.cur = self.conn.cursor() print(’mysql conn success!’)except: print('mysql conn error!') def __del__(self):#if self.cur:# self.cur.close()if self.conn: self.conn.close()if __name__ == ’__main__’: a = A()
補(bǔ)充:事實(shí)證明,不能在__del__()里面close游標(biāo)
回答2:可能是你配置寫(xiě)錯(cuò)了,沒(méi)連接上mysql如果是這句代碼self.cur = self.conn.cursor()報(bào)的錯(cuò),說(shuō)明你沒(méi)連接上
相關(guān)文章:
1. javascript - immutable配合react提升性能?2. javascript - sublime快鍵鍵問(wèn)題3. javascript - nodejs關(guān)于進(jìn)程間發(fā)送句柄的一點(diǎn)疑問(wèn)4. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪問(wèn)?5. 實(shí)現(xiàn)bing搜索工具urlAPI提交6. javascript - 移動(dòng)端上不能實(shí)現(xiàn)拖拽布局嗎?7. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽(tīng)數(shù)據(jù)變化8. phpstudy8.1支持win11系統(tǒng)嗎?9. 配置Apache時(shí),添加對(duì)PHP的支持時(shí)語(yǔ)法錯(cuò)誤10. css - 寫(xiě)頁(yè)面遇到個(gè)布局問(wèn)題,求大佬們幫解答,在線等,急!~

網(wǎng)公網(wǎng)安備