python django 原生sql 獲取數(shù)據(jù)的例子
如下所示:
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 方法和模塊級(jí)方法里,你可以自由的執(zhí)行自定義SQL語(yǔ)句. 對(duì)象 django.db.connection 表示當(dāng)前的數(shù)據(jù)庫(kù)連接. 調(diào)用connection.cursor() 得到一個(gè)游標(biāo)對(duì)象. 然后調(diào)用 cursor.execute(sql, [params])``以執(zhí)行 SQL 語(yǔ)句, 使用 ``cursor.fetchone() 或cursor.fetchall() 得到結(jié)果集. 下面是一個(gè)例子: 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語(yǔ)句改變了數(shù)據(jù)庫(kù)中的數(shù)據(jù) -- 比如你使用了 DELETE 或 UPDATE 語(yǔ)句. 你需要調(diào)用 connection.commit() 來(lái)使你的修改生效. 例子: 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 獲取數(shù)據(jù)的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Django自帶的用戶驗(yàn)證系統(tǒng)實(shí)現(xiàn)
這篇文章主要介紹了Django自帶的用戶驗(yàn)證系統(tǒng)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12python求兩個(gè)時(shí)間的時(shí)間差(實(shí)例代碼)
我們?cè)谟胮ython進(jìn)行分析的時(shí)候,可能會(huì)碰到計(jì)算兩個(gè)日期的時(shí)間差。下面為大家介紹一下如何計(jì)算兩個(gè)時(shí)間的時(shí)間差,需要的朋友可以參考下2022-11-118個(gè)實(shí)用的Python程序你知道幾個(gè)
這篇文章主要為大家詳細(xì)介紹了8個(gè)實(shí)用的Python程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助<BR>2022-02-02django 認(rèn)證類配置實(shí)現(xiàn)
本文主要介紹了django 認(rèn)證類配置實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Django admin 實(shí)現(xiàn)search_fields精確查詢實(shí)例
這篇文章主要介紹了Django admin 實(shí)現(xiàn)search_fields精確查詢實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03利用Python實(shí)現(xiàn)無(wú)損GIF動(dòng)圖的制作
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)無(wú)損GIF動(dòng)圖的制作,文中的實(shí)現(xiàn)方法講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,需要的可以參考一下2023-04-04python實(shí)現(xiàn)簡(jiǎn)易連點(diǎn)器
本文主要介紹了python實(shí)現(xiàn)簡(jiǎn)易連點(diǎn)器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Python3.4學(xué)習(xí)筆記之常用操作符,條件分支和循環(huán)用法示例
這篇文章主要介紹了Python3.4常用操作符,條件分支和循環(huán)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python3.4常見的數(shù)學(xué)運(yùn)算、邏輯運(yùn)算操作符,條件分支語(yǔ)句,循環(huán)語(yǔ)句等功能與基本用法,需要的朋友可以參考下2019-03-03使用python實(shí)現(xiàn)CNN-GRU故障診斷的代碼示例
這篇文章主要給大家詳細(xì)介紹了如何使用python實(shí)現(xiàn)CNN-GRU故障診斷,文章中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07Python?jieba庫(kù)的安裝詳細(xì)圖文教程
jieba庫(kù)的作用主要用于中文分詞,是一種不錯(cuò)的中文分詞組件,下面這篇文章主要給大家介紹了關(guān)于Python?jieba庫(kù)安裝的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03