Mysqldb AttributeError: cursor

余生长醉 提交于 2019-12-11 03:32:41

问题


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.



来源:https://stackoverflow.com/questions/5755837/mysqldb-attributeerror-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!