经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Django » 查看文章
python django 原生sql 获取数据的例子
来源:jb51  时间:2019/8/15 9:54:23  对本文有异议

如下所示:

  1. node2:/django/mysite/blog#cat views.py
  2. 1,
  3. # -*- coding: utf-8 -*-
  4. from __future__ import unicode_literals
  5. # from django.shortcuts import render, render_to_response
  6. from .models import *
  7. # Create your views here.
  8. from django.http import HttpResponse
  9. from django.template import loader
  10. import MySQLdb
  11. def query():
  12. conn= MySQLdb.connect(
  13. host='localhost',
  14. port = 3306,
  15. user='root',
  16. passwd='1234567',
  17. db ='tlcb',
  18. )
  19. cur = conn.cursor()
  20. a=cur.execute("select title,body, DATE_FORMAT(timestamp,'%Y~%m~%d %k.%i.%s') A from blog_blogpost")
  21. info = cur.fetchall()
  22. return info
  23. cur.close()
  24. conn.close()
  25. def archive(req):
  26. print 'aaaaaaaaaaaaaaaaaaaaaaaaaaa'
  27. print req
  28. print type(req)
  29. print req.GET
  30. print '#############################'
  31. print req.GET['aa']
  32. print req.GET['cc']
  33. print '#############################'
  34. print 'aaaaaaaaaaaaaaaaaaaaaaaaaaa'
  35. # get all blogpost objects
  36. posts =query()
  37. print posts
  38. print type(posts)
  39. #print blog_list
  40. template = loader.get_template('archive.html')
  41. context = {
  42. 'posts':posts
  43. }
  44. print '------------------------------------------'
  45. print HttpResponse(template.render(context, req))
  46. print '------------------------------------------'
  47. return HttpResponse(template.render(context, req))
  48. node2:/django/mysite/blog#
  49. node2:/django/mysite/blog/templates#vi archive.html
  50. node2:/django/mysite/blog/templates#
  51. node2:/django/mysite/blog/templates#
  52. node2:/django/mysite/blog/templates#
  53. node2:/django/mysite/blog/templates#cat archive.html
  54. {% extends "base.html" %}
  55. {% block content %}
  56. {% for post in posts %}
  57. <h2>{{ post.0 }}</h2>
  58. <p>{{ post.1 | date:"1,F jS"}}</p>
  59. <p>{{ post.2 }}</p>
  60. {% endfor %}
  61. {% endblock %}
  62. (('dd', 'ddd', '2017~11~24 8.31.42'), ('66666666', '66666', '2017~11~24 8.35.25'), ('777777777', '77777777777', '2017~11~27 1.46.15'))
  63. <type 'tuple'>
  64. 在自定义 model 方法和模块级方法里,你可以自由的执行自定义SQL语句. 对象 django.db.connection 表示当前的数据库连接. 调用connection.cursor() 得到一个游标对象. 然后调用 cursor.execute(sql, [params])``以执行 SQL 语句, 使用 ``cursor.fetchone() cursor.fetchall() 得到结果集. 下面是一个例子:
  65. def my_custom_sql(self):
  66. from django.db import connection
  67. cursor = connection.cursor()
  68. cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
  69. row = cursor.fetchone()
  70. return row
  71. 如果你的SQL语句改变了数据库中的数据 -- 比如你使用了 DELETE UPDATE 语句. 你需要调用 connection.commit() 来使你的修改生效.
  72. 例子:
  73. def my_custom_sql2(self):
  74. from django.db import connection
  75. cursor = connection.cursor()
  76. cursor.execute("DELETE FROM bar WHERE baz = %s", [self.baz])
  77. connection.commit()

以上这篇python django 原生sql 获取数据的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号