【问题标题】:MySQLdb can't use cursorclassMySQLdb 不能使用游标类
【发布时间】:2011-05-28 09:24:11
【问题描述】:

我正在尝试执行以下代码:

import MySQLdb
import MySQLdb.cursors
conn=MySQLdb.connect(host = '127.0.0.1',
                     user = 'root',
                     passwd = 'root',
                     db = 'test',
                     cursorclass = MySQLdb.cursors.DictCursor)
cursor=conn.cursor()

但它给了我以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 243, in cursor
  AttributeError: 'Connection' object has no attribute 'cursorclass'

这是为什么?

【问题讨论】:

  • 您的代码对我有用,但我发现路径 build/bdist.linux-x86_64/egg 很奇怪 - 您是在构建目录之外运行代码吗?如果您安装在 virtualenv 中,代码是否有效?

标签: python mysql-python


【解决方案1】:
 import MySQLdb
 import MySQLdb.cursors
 conn=MySQLdb.connect(host = '127.0.0.1',
                         user = 'root', 
                         passwd = 'root',
                         db = 'test',)
cursor=conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)

这是一个粗略的 hack,但可能不建议使用它。默认情况下抛出字典对象的脚本,但应用程序可能需要元组或数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多