MySQLdb不能使用cursorclass

时间:2011-05-28 09:24:12

标签: python mysql-python

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

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'

为什么会这样?

1 个答案:

答案 0 :(得分:12)

 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)

这是一个粗略的黑客攻击,但使用它可能并不明智。默认情况下抛出字典对象的脚本,但应用程序可能需要元组或数组。