我正在尝试执行以下代码:
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'
这是为什么?
最佳答案
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,但不建议使用它。默认情况下,脚本会抛出字典对象,但应用程序可能需要元组或数组。
关于python - MySQLdb 不能使用游标类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6160761/