溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python中django原生sql如何獲取數據

發布時間:2021-05-22 10:00:42 來源:億速云 閱讀:279 作者:小新 欄目:開發技術

這篇文章主要介紹python中django原生sql如何獲取數據,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

如下所示:

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 %}
   <h3>{{ post.0 }}</h3>
   <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主要用來做什么

Python主要應用于:1、Web開發;2、數據科學研究;3、網絡爬蟲;4、嵌入式應用開發;5、游戲開發;6、桌面應用開發。

以上是“python中django原生sql如何獲取數據”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女