python - weakly-referenced object no longer exists? 數(shù)據(jù)庫連接關(guā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ù)庫連接也成功了,但是執(zhí)行完該類后就會報錯:
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
還請知道什么原因的司機(jī)解惑!萬分感謝!按stackoverflow上的方案就是我寫的這樣,但是還是有問題,難道是我的翻譯軟件有問題?
問題解答
回答1:好吧,自己答一下。并不知道是什么原因?qū)е碌腻e誤,等以后水平高點(diǎn)再來回答。暫時解決辦法:
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:可能是你配置寫錯了,沒連接上mysql如果是這句代碼self.cur = self.conn.cursor()報的錯,說明你沒連接上
相關(guān)文章:
1. 在mac下出現(xiàn)了兩個docker環(huán)境2. css3 - css怎么實(shí)現(xiàn)圖片環(huán)繞的效果3. android - 用textview顯示html時如何寫imagegetter獲取網(wǎng)絡(luò)圖片4. javascript - 原生canvas中如何獲取到觸摸事件的canvas內(nèi)坐標(biāo)?5. css - 定位為absolute的父元素中的子元素 如何設(shè)置在父元素的下面?6. JavaScript事件7. javascript - jquery hide()方法無效8. 網(wǎng)頁爬蟲 - 用Python3的requests庫模擬登陸B(tài)ilibili總是提示驗(yàn)證碼錯誤怎么辦?9. 注冊賬戶文字不能左右分離10. html - vue項(xiàng)目中用到了elementUI問題
