Mysqldb 属性错误:游标
我开始在 python 中使用 mysqldb 模块,并且我似乎对调用查询的“标准”方式有一些问题。
我知道标准方法是创建游标,然后用它来执行查询。
然而,当我尝试实例化一个时,它给出了以下错误:
属性错误:光标
我的数据库类看起来像:
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()
现在我通过使用查询方法让它工作,但我觉得不使用标准将来会给我带来麻烦。
有什么想法吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用了错误的连接构造函数。
MySQLdb.Connection
而不是MySQLdb.connection
应该可以工作。You are using wrong connection constructor.
MySQLdb.Connection
instead ofMySQLdb.connection
should work.