簡體   English   中英

Python:dbcur.execute AttributeError:'NoneType'對象沒有屬性'execute'

[英]Python: dbcur.execute AttributeError: 'NoneType' object has no attribute 'execute'

我正在嘗試執行以下代碼:

def getServerSetting(self, setting):
    self.dbcur.execute('select value from settings where setting = ?', [str(setting)])
    rrf = self.dbr.fetchone()
    if rrf is None:
        return False
    else:
        return rrf[0]

但是,運行python腳本時出現此錯誤:

dbcur.execute('select value from settings where setting = ?', [str(setting)])
AttributeError: 'NoneType' object has no attribute 'execute'

嘗試這樣編寫函數。 如果仍然有錯誤,請告訴我。

def getServerSetting(self, setting):

    if not self.dbcon:
        self.dbcon = MySQLdb.connect(host='localhost', user='removed', passwd='removed', db='removed', port=21)
    self.dbcur = self.dbcon.cursor()

    self.dbcur.execute('select value from settings where setting = ?', [str(setting)])
    rrf = self.dbr.fetchone()
    if rrf is None:
            return False
    else:
            return rrf[0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM