django中使用原生SQL语句

django中使用原生SQL语句,第1张

概述views中代码; # 有多个数据库时from django.db import connections# 傳入游标,得到字典結果集def dictfetchall(cursor): "将游标返回的结果保存到一个字典对象中" desc = cursor.description return [ dict(zip([col[0] for col in desc 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 >            <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>
总结

以上是内存溢出为你收集整理的django中使用原生SQL语句全部内容,希望文章能够帮你解决django中使用原生SQL语句所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1191412.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存