Mysqldb AttributeError:游标 [英] Mysqldb AttributeError: cursor
问题描述
我开始在python中使用mysqldb模块,并且我似乎对调用查询的标准"方式有一些疑问.
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:光标
AttributeError: cursor
我的数据库类如下:
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.
有什么主意吗?
推荐答案
您使用了错误的连接构造函数.
You are using wrong connection constructor.
MySQLdb.Connection
而不是MySQLdb.connection
应该可以工作.
MySQLdb.Connection
instead of MySQLdb.connection
should work.
这篇关于Mysqldb AttributeError:游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!