Mysqldb AttributeError: cursor

2019-07-16 17:24发布

问题:

I am starting to use the mysqldb module in python and I seem to have some issues with the "standard" way of calling queries.

I understand that the standard way is to create a cursor and then use it to execute queries.

However, when I try to instanciate one, it gives me the following error :

AttributeError: cursor

My Database class looks like :

class Database():

    def __init__(self):
        server = "localhost"
        login = "login"
        password = "passws"
        database = "DB"
        my_conv = { FIELD_TYPE.LONG: int }

        self.conn = MySQLdb.connection(user=login, passwd=password, db=database, host=server, conv=my_conv)
        self.cursor = self.conn.cursor()

    def close(self):
        self.conn.close()

    def execute(self, query):
        self.cursor.execute(query)
        return self.cursor.fetchall()

For now I get it working by using the query method, but I feel not using the standard will give me trouble in the future.

Any idea ?

回答1:

You are using wrong connection constructor.

MySQLdb.Connection instead of MySQLdb.connection should work.