Python:dbcur.execute AttributeError:'NoneType'对象没有属性'execute'

时间:2014-07-23 04:09:54

标签: python attributes nonetype

我正在尝试执行这段代码:

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'

1 个答案:

答案 0 :(得分:1)

尝试编写这样的函数。如果仍有错误,请告诉我。

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]