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")
# 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")
相关文章推荐
- django通过ajax发起请求返回JSON格式数据的方法
- json格式数据,将数据库中查询的结果转换为json, 然后调用接口的方式返回json(方式一)
- php从数据库读取数据,并以json格式返回数据的方法
- Android访问网络系列之--服务端返回XML或JSON格式数据,Android 进行解析并显示
- php执行数据库查询返回json格式数据
- django-将数据库数据转换成JSON格式(ORM和SQL两种情况)
- django返回json格式的数据的方法
- django 返回json格式数据
- django返回json格式的数据的方法
- php执行数据库查询返回json格式数据
- JDBC- exercise-2 通过htttp 访问 数据库,返回一个JSON格式的相应信息
- ajax根据ID查询数据库并返回Json格式数据返回js,使用append显示到页面。判断json值为[]或者[[]]的问题。
- php从数据库读取数据,并以json格式返回数据
- Struts2 使用ajax访问action,返回json格式数据,提示error:There is no result type defined for type 'json'
- django通过ajax发起请求返回JSON格式数据的方法
- Android访问网络系列之--服务端返回XML或JSON格式数据,Android 进行解析并显示
- php执行数据库查询返回json格式数据
- Android访问网络:服务端返回XML或JSON格式数据,Android 进行解析并使用ListView显示
- Dojo AJAX 访问C#Web 服务 并且返回JS 格式的数据
- Asp.net无刷新调用后台实体类数据并以Json格式返回