'长'对象没有属性'fetchall'
我不知道这段代码有什么问题; 以前它工作正常,但在数据库迁移(sqlite3到MySQL)后,它不再有效。 (我正在使用MySQL)。
Traceback:在get_response 111中的文件“/usr/lib/python2.6/site-packages/django/core/handlers/base.py”。response = callback(request,* callback_args,** callback_kwargs)File“/ usr / lib /python2.6/site-packages/django/contrib/auth/decorators.py“在_wrapped_view中23. return view_func(request,* args,** kwargs)
码:
cursor = connection.cursor()
data = cursor.execute(query)
data_list = data.fetchall()
return redirect("http://www.example.com?code=123" , code=302)
result_count = len(data_list)
if result_count==0:
return HttpResponse('<script type="text/javascript"> alert ("try again"); window.location.href = "/reports/custom/";</script>')
data_desc = data.description
j=0
"""
prepare first response
"""
now = datetime.datetime.now().strftime('%m-%d-%Y_%H:%M:%S')
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=hiv_report_%s.csv' % now
writer = csv.writer(response)
headers = []
tab_no = 0
for i in data_desc:
#ws.write(0, j, (i[0].replace('_', ' ')).upper())
if i[0] == 'id':
table_name = tab_order[tab_no]
tab_no = tab_no +1
headers.append((table_name+ " | " +i[0].replace('_', ' ')).upper())
writer.writerow(headers)
"""
fill data into csv cells
"""
for value in data_list:
k=0
no_record_check=1
row = []
for val in value:
#ws.write(j, k, val)
row.append(val)
writer.writerow(row)
MySQLdb.cursor.execute(query)
返回一个带有返回行数的整数。 数字对象没有fetchall
方法。 您需要在cursor
上调用fetchall
方法:
data_list = cursor.fetchall()
引用Python DB API:
.execute(operation [, parameters])
Prepare and execute a database operation (query or command).
[...]
Return values are not defined.
正如Martijn在评论中所说的那样, sqlite3.cursor.execute
返回游标。 由于cursor.execute
返回值没有由DB API定义MySQLdb.cursor.execute
可以返回任何内容(库编写者选择返回多行)。
这意味着使用Python DB API的可移植方式是忽略cursor.execute
的返回值。
上一篇: 'long' object has no attribute 'fetchall'
下一篇: Using Moq to mock an asynchronous method for a unit test