MySQLdb无法使用cursorclass

4 投票
1 回答
5944 浏览
提问于 2025-04-16 18:31

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

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 个回答

13
 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)

这是一种粗略的解决办法,但可能不太建议使用。这个脚本默认会抛出一个字典对象,但应用程序可能需要的是一个元组或数组。

撰写回答