标签:根据 for check top etc 语句 tip exe fetchall
views中代码;# 有多个数据库时
from django.db import connections
# 傳入游标,得到字典結果集
def dictfetchall(cursor):
"将游标返回的结果保存到一个字典对象中"
desc = cursor.description
return [
dict(zip([col[0] for col in desc], row))
for row in cursor.fetchall()
]
def seldata(request):
sql ="SELECT TOP 100 * FROM EwData"
conn = connections[‘MyDB‘] #連接的數據庫
cur = conn.cursor() #連接游標
cur.execute(sql) #執行SQL語名
data = dictfetchall(cur) #把結果用字典返回
return render(request, ‘ewdata.html‘, {‘ew‘: data,‘fcol‘:data[0]})
html代码:
<table class="gridtable">
<tr>
{% for k in fcol %}
<th>{{ k }}</th>
{% endfor %}
</tr>
{% for i in ew %}
<tr>
{# 根据SQL中的字段名显示数据 #}
<td>{{ i.McNo }}</td>
<td>{{ i.SN }}</td>
<td>{{ i.Model }}</td>
<td>{{ i.Block }}</td>
<td>{{ i.Floor }}</td>
<td>{{ i.Line }}</td>
<td>{{ i.Wight }}</td>
<td>{{ i.TestTime }}</td>
<td>{{ i.Abortive }}</td>
<td>{{ i.Checker }}</td>
<td>{{ i.Multiple }}</td>
<td>{{ i.PackSN }}</td>
</tr>
{% endfor %}
</table>
标签:根据 for check top etc 语句 tip exe fetchall
原文地址:https://blog.51cto.com/alun51cto/2437342