'NoneType'对象没有Mysql Connector的属性'fetchall'

时间:2019-12-30 20:44:38

标签: mysql python-3.x mysql-connector-python

我正在尝试通过mysql DB执行select语句,当我尝试在DB客户端上运行它时,我的查询工作正常,但是使用我的代码却出现以下错误,我正在使用MYSQL连接器执行以下代码

  

“ NoneType”对象没有属性“ fetchall”

以下是我的代码

def execute_sql(self, sql, params=None, scalar=False):
    if self.connection is None:
        self.open_connection()
    cursor = self.connection

    # todo: remove commit from here, and make the implementation part of the
    # calling code

    results = cursor.execute(sql, params).fetchall()

    self.myDb.commit()
    return results

def open_connection(self):
    """
    Opens the database connection using the saved credentials.
    """
    self.myDb = mysql.connector.connect(
        host=self.hostname,
        user=self.username,
        passwd=self.password,
        database=self.database
    )
    self.connection = self.myDb.cursor()

0 个答案:

没有答案