日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python django 原生sql 獲取數據的例子

系統 1790 0

如下所示:

            
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 %}
   
            

{{ post.0 }}

{{ post.1 | date:"1,F jS"}}

{{ post.2 }}

{% 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')) 在自定義 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 獲取數據的例子就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 珠海市| 台中市| 达尔| 镇巴县| 柘荣县| 绵阳市| 自治县| 巴楚县| 磐安县| 宁蒗| 丘北县| 察雅县| 合水县| 晴隆县| 博兴县| 神木县| 鹤峰县| 阿鲁科尔沁旗| 壤塘县| 尚志市| 凤庆县| 达拉特旗| 炉霍县| 兰坪| 外汇| 射阳县| 景德镇市| 红原县| 海阳市| 蓬莱市| 九江市| 会泽县| 宜兰县| 白朗县| 麻城市| 瑞安市| 黑山县| 钦州市| 襄樊市| 宝应县| 秭归县|