您的位置:首页 > 数据库

django 访问数据库返回 JSON数据格式

2017-06-13 00:00 676 查看
from django.shortcuts import render

# Create your views here.

from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response

# Create your views here.
import cx_Oracle as oracle
import json
def search_db(sqlcmd):
conn = oracle.connect('test/test@test:1526/orcl')
cur = conn.cursor()
cur.execute(sqlcmd)
result = cur.fetchall()
# print ("Exec sql:%s"%(sqlcmd))
conn.close()
return result
def cont_json_format(dbname, USED_SIZE, FREE_SIZE,SM_SIZE,OTIME):
json_str = {"code":200, "message": "OK", "descriptions": {"DB_NAME":dbname, "USED_SIZE": USED_SIZE, "FREE_SIZE": FREE_SIZE, "SM_SIZE":SM_SIZE, "OTIME":OTIME}}
return json.dumps(json_str)

def cont(request,dbname):
starttime = request.GET['starttime']
endtime = request.GET['endtime']
sql='''select *
from ORADB_SIZE t
where
t.DB_NAME = '%s' and
t.OTIME > to_date('%s','YYYY/MM/DD') and
t.OTIME < to_date('%s','YYYY/MM/DD')''' % (dbname, starttime, endtime)
result = search_db(sql)
USED_SIZE = [round(e[1],2) for e in result]
FREE_SIZE = [round(e[2],2) for e in result]
SM_SIZE = [round(e[3],2) for e in result]
OTIME = [e[4].strftime('%Y-%m-%d') for e in result]
json_data = cont_json_format(dbname, USED_SIZE, FREE_SIZE, SM_SIZE, OTIME)
return HttpResponse(json_data)

def echarts_cont(request):
return render_to_response("content.html")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: