python django 原生sql 获取数据的例子

yipeiwu_com6年前Python基础

如下所示:

node2:/django/mysite/blog#cat views.py
1,
 
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# from django.shortcuts import render, render_to_response
from .models import *
# Create your views here.
from django.http import HttpResponse
from django.template import loader
import MySQLdb
 
def query():
  conn= MySQLdb.connect(
    host='localhost',
    port = 3306,
    user='root',
    passwd='1234567',
    db ='tlcb',
    )
  cur = conn.cursor()
  a=cur.execute("select title,body, DATE_FORMAT(timestamp,'%Y~%m~%d %k.%i.%s') A from blog_blogpost")
  info = cur.fetchall()
  return info
  cur.close()
  conn.close()
 
def archive(req):
 print 'aaaaaaaaaaaaaaaaaaaaaaaaaaa'
 print req
 print type(req)
 print req.GET
 print '#############################'
 print req.GET['aa']
 print req.GET['cc']
 print '#############################'
 print 'aaaaaaaaaaaaaaaaaaaaaaaaaaa'
# get all blogpost objects
 posts =query() 
 print posts
 print type(posts)
 #print blog_list
 template = loader.get_template('archive.html')
 context = {
 'posts':posts
 }
 print '------------------------------------------'
 print HttpResponse(template.render(context, req))
 print '------------------------------------------'
 return HttpResponse(template.render(context, req))
node2:/django/mysite/blog#
 
 
node2:/django/mysite/blog/templates#vi archive.html
node2:/django/mysite/blog/templates#
node2:/django/mysite/blog/templates#
node2:/django/mysite/blog/templates#
node2:/django/mysite/blog/templates#cat archive.html
{% extends "base.html" %} 
{% block content %}
   {% for post in posts %}
   <h2>{{ post.0 }}</h2>
   <p>{{ post.1 | date:"1,F jS"}}</p>
   <p>{{ post.2 }}</p>
   {% endfor %}
 {% endblock %}
 
 
 
(('dd', 'ddd', '2017~11~24 8.31.42'), ('66666666', '66666', '2017~11~24 8.35.25'), ('777777777', '77777777777', '2017~11~27 1.46.15'))
<type 'tuple'>
 
 
 
 
 
 
 在自定义 model 方法和模块级方法里,你可以自由的执行自定义SQL语句. 对象 django.db.connection 表示当前的数据库连接. 调用connection.cursor() 得到一个游标对象. 然后调用 cursor.execute(sql, [params])``以执行 SQL 语句, 使用 ``cursor.fetchone() 或cursor.fetchall() 得到结果集. 下面是一个例子:
def my_custom_sql(self): 
  from django.db import connection 
  cursor = connection.cursor() 
  cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz]) 
  row = cursor.fetchone() 
  return row 
 
    如果你的SQL语句改变了数据库中的数据 -- 比如你使用了 DELETE 或 UPDATE 语句. 你需要调用 connection.commit() 来使你的修改生效.
例子:
def my_custom_sql2(self): 
  from django.db import connection 
  cursor = connection.cursor() 
  cursor.execute("DELETE FROM bar WHERE baz = %s", [self.baz]) 
  connection.commit() 

以上这篇python django 原生sql 获取数据的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Windows下将Python文件打包成.EXE可执行文件的方法

 在使用Python做开发的时候,时不时会给自己编写了一些小工具辅助自己的工作,但是由于开发依赖环境问题,多数只能在自己电脑上运行,拿到其它电脑后就没法运行了。这显得很不方便,不符合我们...

在python中实现对list求和及求积

如下所示: # the basic way s = 0 for x in range(10): s += x # the right way s = sum(range(10))...

python flask实现分页的示例代码

结合mysql数据库查询,实现分页效果 @user.route("/user_list",methods=['POST','GET']) def user_list(): p =...

使用Python在Windows下获取USB PID&VID的方法

在Linux系统下获取USB PID&VID是件十分容易的事情,只需要"lsusb"命令就可以了。 不过,对于Windows,就没有那么容易了。 之前,有尝试过通过注册表来获得目前连接d...

对python文件读写的缓冲行为详解

文件的io操作的缓冲行为分为 全缓冲:同系统及磁盘块大小有关,n个字节后执行一次写入操作 行缓冲:遇到换行符执行一次写操作 无缓冲:立刻执行写操作 open()函数 help(ope...