'long' object has no attribute 'fetchall'
I don't know what is wrong in this code; previously it was working fine but after a database migration (sqlite3 to MySQL) it is no longer working. (I am using MySQL).
Traceback: File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/usr/lib/python2.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 23. return view_func(request, *args, **kwargs)
code:
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)
returns an integer with the number of returned rows. Number objects don't have fetchall
method. You need to call fetchall
method on a cursor
:
data_list = cursor.fetchall()
To quote Python DB API:
.execute(operation [, parameters])
Prepare and execute a database operation (query or command).
[...]
Return values are not defined.
As Martijn said in the comment sqlite3.cursor.execute
returns cursor. Since return value of cursor.execute
is not defined by DB API MySQLdb.cursor.execute
can return anything (the library writers chose to return a number of rows).
This means that the portable way of working with Python DB API is to ignore the return value of cursor.execute
.
上一篇: Django异常DoesNotExist
下一篇: '长'对象没有属性'fetchall'